Android學習筆記 RelativeLayout,TableLayout佈局

一.RelativeLayout相對佈局方式.

 

       RelativeLayout顧名思義,相對佈局,在這個容器內部的子元素們可以使用彼此之間的相對位置或者和容器間的相對位置來進行定位。

 

       註意:不能在RelativeLayout容器本身和他的子元素之間產生循環依賴,比如說,不能將RelativeLayout的高設置成為WRAP_CONTENT的時候將子元素的高設置成為 ALIGN_PARENT_BOTTOM。

 

    RelativeLayout相關的佈局屬性:

 

     android:layout_above 將該控件置於給定ID的控件之上

 

     android:layout_below 將該控件的置於給定ID控件之下

 

     android:layout_toLeftOf 將該控件置於給定ID的控件之左

 

     android:layout_toRightOf 將該控件置於給定ID的控件之右

 

RelativeLayout佈局中相對於父控件來說位置屬性:

 

                 android:layout_alignParentLeft 如果為True,該控件位於父控件的左部

 

                 android:layout_alignParentRight 如果為True,該控件位於父控件的右部

 

                 android:layout_alignParentTop 如果為True,該控件位於父控件的頂部

 

                 android:layout_alignParentBottom 如果為True,該控件位於父控件的底部

 

RelativeLayout佈局時對齊相關的屬性:

 

                 android:layout_alignBaseline 該控件基線對齊給定ID的基線

 

                 android:layout_alignBottom 該控件於給定ID的控件底部對齊

 

                 android:layout_alignLeft 該控件於給定ID的控件左對齊

 

                 android:layout_alignRight 該控件於給定ID的控件右對齊

 

                 android:layout_alignTop 該控件於給定ID的控件頂對齊

 

 

 

                 android:layout_centerHorizontal 如果為True,該控件將被置於水平方向的中央

 

                 android:layout_centerInParent 如為Ture,該控件將被置於父控件水平方向和垂直方向

 

                 android:layout_centerVertical 如果為True,該控件將被置於垂直方向的中央

 

Relative佈局一:效果圖:RelativeLayoutOne佈局

 

 

 

佈局源碼:

 

 

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"

    xmlns:tools="https://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="10px"

    android:background="#00aa00"

    tools:context=".RelativeLayoutActivity" >

 

    <TextView

        android:id="@+id/tv_heard"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:textSize="12pt"

        android:text="XXX系統登錄界面" />

    <TextView android:id="@+id/tv_account"

        android:layout_height="wrap_content"

        android:layout_width="wrap_content"

        android:layout_below="@id/tv_heard"

        android:layout_marginTop="30px"

        android:text="用戶名:"/>

    <EditText android:id="@+id/txt_account"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/tv_heard"

        android:layout_toRightOf="@id/tv_account"

        android:layout_alignBaseline="@id/tv_account"/>

    <TextView android:id="@+id/tv_pwd"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@id/tv_account"

        android:layout_marginTop="30px"

        android:text="密    碼:"/>

    <EditText android:id="@+id/txt_pwd"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/txt_account"

        android:layout_toRightOf="@id/tv_pwd"

        android:layout_alignBaseline="@id/tv_pwd"/>

   <Button android:id="@+id/btn_exit"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_below="@id/txt_pwd"

       android:layout_alignParentRight="true"

       android:layout_marginTop="30px"

       android:text="退出"/>

    <Button android:id="@+id/btn_login"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_below="@id/txt_pwd"

       android:layout_toLeftOf="@id/btn_exit"

       android:layout_marginTop="30px"

       android:layout_marginRight="50px"

       android:text="登錄"/>

</RelativeLayout>

 

RelativeLayoutSecond佈局效果圖:使用 LinearLayout和 RelativeLayout結合佈局更加快捷方便。

 

 

 

佈局源碼:

 

 

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"

    xmlns:tools="https://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#00aaff"

    android:padding="10px"

    tools:context=".RelativeLayoutSecondActivity" >

  <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:textSize="12pt"

        android:text="XXX系統登錄界面" />

<LinearLayout android:id="@+id/linear_one"

    android:layout_below="@id/tv_heard"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:orientation="horizontal">

      <TextView 

        android:layout_height="wrap_content"

        android:layout_width="wrap_content"

        android:text="用戶名:"/>

      <EditText 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"/>

</LinearLayout>

<LinearLayout android:id="@+id/linear_second"

    android:layout_height="wrap_content"

    android:layout_width="fill_parent"

    android:orientation="horizontal"

    android:layout_below="@id/linear_one">

    <TextView 

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="密    碼:"/>

    <EditText  android:layout_width="fill_parent"

        android:layout_height="wrap_content"/>

</LinearLayout>

<LinearLayout android:id="@+id/linear_third"

    android:layout_height="wrap_content"

    android:layout_width="fill_parent"

    android:orientation="horizontal"

    android:layout_below="@id/linear_second"

    android:gravity="right"> 

     <Button 

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="登錄"/>

     <Button 

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_marginLeft="30px"

       android:text="退出"/>

</LinearLayout>

</RelativeLayout>

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *