# Android布局

Framelayout

  • 框架布局

    框架布局是用來占據(jù)屏幕一塊區(qū)域來顯示一個單一的item.通常,框架布局應(yīng)該擁有單一的child view,因為很難去管理多個child view,當你去適配views防止這些child view 相互覆蓋的時候.當然,你也可以添加多個children到框架布局中,通過控制他們的位置即框架布局分配gravity到每一個child view.

    通過layout_gravity設(shè)定位置.通過layout_margin來設(shè)置間距

     <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical" android:layout_width="match_parent"
         android:layout_height="match_parent">
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="居中"
             android:layout_gravity="center"/>
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="左邊"
             android:layout_gravity="left"/>
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="距離頂部20dp,右邊20dp"
             android:layout_marginTop="20dp"
             android:layout_marginLeft="20dp"
             android:layout_marginStart="20dp"/>
     </FrameLayout>  
    

AbsoluteLayout

  • 絕對布局(google不建議使用絕對布局)

能夠使子View在某一個固定的位置.絕對布局缺乏靈活性,而且很難去維護相比于其他沒有絕對位置的布局.

     <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="100dp"
            android:layout_y="100dp"
            android:text="文本"
            />
    </AbsoluteLayout>

Relativelayout

  • 相對布局

布局位置可以根據(jù)周圍相互關(guān)聯(lián)的布局或者父布局來設(shè)置.

官方提示:

Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

不能用于一個循壞依賴的布局在相對布局尺寸和子布局之間.舉個例子,當你Relativelayout的高度是WRAP_CONTENT,那么Relativelayout的尺寸是相對的,這個時候你設(shè)置子View 為ALIGN_PARENT_BOTTOM,也是依賴于父布局的底邊的位置,這樣是不行的.

在android系統(tǒng)版本在17級以下(包含17的時候)。使用measure會出現(xiàn)NULL異常情況,這個是一個BUG。原因是在RelativeLayout的控件使用在含有scrolling的時候,該含有scrolling的控件中計算空間大小的時候,沒有使用MeasureSpec mode UNSPECIFIED的布局方式在RelativeLayout。自定義的控件則會盡可能的使用AT_MOST 來替換對齊方式。

如果你想解決這個問題有2個方法:

1.講SDK的目標版本升級

2.將需要使用RelativeLayout的上層包一個LinearLayout即可.

  • 甲在乙的____邊
屬性 解釋
Above 定義將元素的底邊對齊另一個元素的頂邊
BELOW 定義將元素的頂邊對齊另一個元素的底邊
  • 甲和乙對齊____邊
屬性 解釋
align_baseline 定義將元素的基線對齊另一個元素的基線
align_bottom 定義將元素的底邊對齊另一個元素的底邊
align_left 定義將元素的左邊對齊另一個元素的左邊
align_right 定義將元素的右邊對齊另一個元素的右邊
align_top 定義將元素的頂邊對齊另一個元素的頂邊
  • 甲對齊父容器__邊
屬性 解釋
align_parent_bottom 定義將元素的底邊對齊父容器的底邊
align_parent_left 定義將元素的左邊對齊父容器的左邊
align_parent_right 定義將元素的右邊對齊父容器的右邊
align_parent_top 定義將元素的頂邊對齊父容器的頂邊
  • 甲在容器__邊
屬性 解釋
CENTER_HORIZONTAL 定義元素在RelativeLayout中水平居中
CENTER_IN_PARENT 定義元素在RelativeLayout的中心
CENTER_VERTICAL 定義元素在RelativeLayout內(nèi)垂直居中
  • 甲的邊對齊已的
屬性 解釋
LEFT_OF 甲元素的右邊對齊乙元素的左邊
RIGHT_OF 甲元素的左邊對齊乙元素的右邊

LinearLayout

  • 線性布局

A Layout that arranges its children in a single column or a single row. The direction of the row can be set by calling setOrientation(). You can also specify gravity, which specifies the alignment of all the child elements by calling setGravity() or specify that specific children grow to fill up any remaining space in the layout by setting the weight member of LinearLayout.LayoutParams. The default orientation is horizontal

一個可以將子元素分布到一列或者一行的布局.可以通過setOrientation來設(shè)置水平或者垂直方向.你也可以通過setGravity來確定重力方向.通過weight屬性來設(shè)置元素在布局中占據(jù)的比例.

  • 線性布局中,Orientation和Gravity存在兩條規(guī)則:

    當 android:orientation="vertical" 時, 只有水平方向的設(shè)置才起作用,垂直方向的設(shè)置不起作用。即:left,right,center_horizontal 是生效的。

    當 android:orientation="horizontal" 時, 只有垂直方向的設(shè)置才起作用,水平方向的設(shè)置不起作用。即:top,bottom,center_vertical 是生效的。

例子:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="文本1"
            android:layout_gravity="end"
            />
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="文本2"
            android:layout_gravity="center_horizontal"
          />
    </LinearLayout>

2:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="文本1"
    android:layout_gravity="top"

    />
<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="文本2"
    android:layout_gravity="bottom"
  />
</LinearLayout>
  • layout_gravity和gravity的區(qū)別

android:gravity是相對于自己的位置,如button中的text相對自己的位置.

android:layout_gravity是相對于布局的位置.如屏幕居中.

android:layout_gravity只在LinearyLayout和FrameLayout中有效.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容