1. 程式人生 > >Android 六大布局之 LinearLayout(線性佈局)

Android 六大布局之 LinearLayout(線性佈局)

LinearLayout( 線性佈局) 檢索

  • Android 中有六大布局: LinearLayout(線性佈局),RelativeLayout(相對佈局),TableLayout(表格佈局) FrameLayout(幀佈局),AbsoluteLayout(絕對佈局),GridLayout(網格佈局)
  • 螢幕適配使用的比較多的就是 LinearLayout 的 weight(權重屬性),本文詳細地解析 LinearLayout,包括一些基本的屬性,Weight 屬性的使用,以及比例如何計算,還有使用 android:divider 繪製下劃線!

  • 本文使用的是 Android Studion 3.1.2 

weight(權重)屬性詳解

最簡單方式

圖 1.1
  • 實現 圖1.1 效果的程式碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#ADFF2F" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:background="#DA70D6" />

</LinearLayout>
  • android:id="@+id/LinearLayout1"  :為該資源控制元件設定一個資源 id,在 Java 程式碼中可以通過 findViewById(id) 找到該控制元件
  • android:layout_width="match_parent":佈局的寬度設定為填滿父容器
  • android:layout_height="match_parent":佈局的高度設定為填滿父容器
  • android:orientation="horizontal":佈局中控制元件的排列方式設定為水平方向
  • android:layout_height="fill_parent:佈局高度設定為充滿父容器
  • tools:context=".MainActivity":聲明當前佈局提供給 activity 使用
  • android:layout_weight:設定權重,按比例劃分水平方向,將涉及到的 View 的 android:layout_width 屬性設定為 0dp,然後使用 android:layout_weight 屬性設定比例即可,如上所示,第二個 LinearLayout 是第一個的兩倍。
  • 以此類推,豎直方向,只需設 android:layout_height 為 0dp,然後設定 layout_weight 屬性即可,如果要達到 1:1 等分效果,則顯然只需要分別把兩個 LinearLayout 的 weight 改成1和1就可以了。
圖 1.2
  • 上圖 1.2 效果程式碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ADFF2F" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#DA70D6" />
</LinearLayout>

wrap_content 方式

  • 如果不用上述最簡單的那種設定為 0dp 的方式,直接用 wrap_content 也是可以的——內容包裹
  • LinearLayout 的 orientation 屬性決定向哪個方向等比例劃分,horizontal(水平)或是 vertical(豎直)
圖 1.3
  • 實現上圖 1.3 效果程式碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#98FB98"
        android:text="one" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:background="#FFFF00"
        android:text="two" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        android:background="#FF00FF"
        android:text="three" />
</LinearLayout>
  • <LinearLayout :表示整個視窗採用線性佈局
  • <TextView:位於線性佈局中的文字控制元件
  • android:orientation:"horizontal":線性佈局採用水平切分
  • <TextView android:layout_width="wrap_content":文字控制元件水平方向採用包裹內容
  • <TextView android:layout_height="fill_parent":文字控制元件垂直方向採用填滿父容器
  •  android:layout_weight="1"、"2"、"3":設定三個文字控制元件的權重,即第二個是第一個兩倍,第三個是第一個的三倍
  • 以此類推,其餘各種比例設定以及垂直方向也是同理。
圖 1.4
  • 上圖 1.4 效果程式碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#98FB98"
        android:text="one" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FFFF00"
        android:text="two" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="#FF00FF"
        android:text="three" />
</LinearLayout>