1. 程式人生 > >RelativeLayout相對佈局和LinearLayout線性佈局屬性

RelativeLayout相對佈局和LinearLayout線性佈局屬性

    LinearLayout(線性佈局)   

            LinearLayout佈局只有兩種排版方式:水平排列和垂直排列。

           LinearLayout用 android:orientation控制控制元件排列的方式:

                   android:orientation="vertical"垂直排列(新建的佈局預設垂直排列)

                   android:orientation="horizontal"水平排列

                  如果LinearLayout是第一個父容器那麼LinearLayout中一定要寫android:orientation語句如果是子容器可以不要,但是子容器預設水平排列。

           layout_gravity控制元件在父容器的位置

                  android:layout_gravity="top"                                  控制元件在父容器的上邊,左右下 left,right,bottom

                  android:layout_gravity="center"                             控制元件在父容器中居中

                  android:layout_gravity="center_vertical"              控制元件在父容器的

垂直方向居中,水平方向居中center_horizontal

                  android:gravity="top"                                              內容在控制元件的上邊      左右下 left,right,bottom   

                  android:gravity="center"                                         內容在控制元件中居中     vertical垂直居中,center_horizontal水平居中

                  android:gravity="bottom|right"                              內容在控制元件的右下方,right|bottom也可以,right|top右上方,left|top左上方

                  android:layout_weight="1"                                     比重,佔佈局的1/總   (注意:在使用weight時如果LinearLayout佈局是orientation="vertical"垂直排列android:layout_height要設定為0dp,orientation="horizontal"時layout_width="0dp",不然佈局會出現問題.

   RelativeLayout(相對佈局)

                 相對佈局顧名思義,控制元件的位置是相對的。RelativeLayout的特點就是控制元件的位置是相對的容易控制。

                 android:layout_toRightOf="@+id/xxx"                             該控制元件在xxx控制元件的右邊

                 android:layout_toLeftOf="@+id/xx"                                  該控制元件在xxx控制元件的左邊

                 android:layout_above="@+id/xxx"                                    該控制元件在xxx控制元件的上邊

                 android:layout_below="@+id/xxx"                                    該控制元件在xxx控制元件的下邊

                 android:layout_alignLeft="@+id/xxx"                               該控制元件的左邊與xxx控制元件的左邊對齊

                 android:layout_alignTop="@+id/xxx"                               該控制元件的上邊與xxx控制元件的上邊對齊

                 android:layout_alignBottom="@+id/xxx"                         該控制元件的下邊與xxx控制元件的下邊對齊

                 android:layout_alignRight="@+id/xxx"                             該控制元件的右邊與xxx控制元件的右邊對齊

                 android:layout_alignParentLeft="true"                             該控制元件的左邊與父控制元件的左邊對齊

                android:layout_alignParentTop="true"                             該控制元件的上邊與父控制元件的上邊對齊

                android:layout_alignParentRight="true"                          該控制元件的右邊與父容器的右邊對齊

               android:layout_alignParentBottom="true"                        該控制元件的下邊與父容器的下邊對齊(這個屬性有的時候應為自己的佈局問題會出現沒有效果的問題,這時候就要重新寫一個佈局包裹這個佈局

                 <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

             <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

                < /RelativeLayout>)

               android:layout_centerInParent="true"                             該控制元件在父控制元件中居中

               android:layout_centerVertical="true"                              該控制元件在父控制元件中垂直居中

                android:layout_centerHorizontal="true"                        該控制元件在父控制元件中水平居中