天天看點

3.2.1 LinearLayout(線性布局)3.2.1 LinearLayout(線性布局)

3.2.1 LinearLayout(線性布局)

标簽: StudyNote

本文聲明:

本文由Coder-pig編寫,想了解其他内容,可見CoderPig’s Android Study Note——目錄

尊重作者勞動成果,未經本人授權,禁止轉載!違者必究!

目錄源位址:http://blog.csdn.net/coder_pig/article/details/51348769

1.基本屬性

  • orientation:設定布局中元件的排放方式,horizontal(水準),vertical(垂直)
  • gravity:控制子元素的對齊方式,可多個組合(left|buttom)
  • layout_gravity:控制改元件在父容器中的對齊方式

2.weight(權重)屬性詳解

核心公式:

View的寬/高 = 原有寬度(android:layout_width) + 剩餘空間的占比!

①推薦用法:把widht/height設定為0dp

按比例劃分水準方向:将涉及到的View的android:width屬性設定為0dp,

然後設定為android weight屬性設定比例即可;類推,豎直方向,隻需設

android:height為0dp,然後設weight屬性即可! 比如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="#03A9F4"
        android:layout_weight="1"
        android:text="11111111111"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="#FFEB3B"
        android:layout_weight="2"
        android:text="B"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="#4CAF50"
        android:layout_weight="3"
        android:text="C"/>

</LinearLayout>
           
3.2.1 LinearLayout(線性布局)3.2.1 LinearLayout(線性布局)

咦,文字左對齊好像不怎麼好看,我們為每個TextView加一個android:gravity=”center”,

剛加一個就發現:

3.2.1 LinearLayout(線性布局)3.2.1 LinearLayout(線性布局)

卧槽,錯位了,什麼鬼,我們為剩下兩個TextView也加上:android:gravity=”center”,

3.2.1 LinearLayout(線性布局)3.2.1 LinearLayout(線性布局)

好吧,其實引起這個原因是baselineAligned屬性引起的,預設為true,就是讓布局控件

中子控件内的文字的基線對齊,是以會出現這個錯位的問題,隻要在外層的LinearLayout

中将這個屬性設定為false即可解決這個問題。

②不建議用法:wrap_content

這個就不怎麼好用了,先配置設定能夠顯示包含他們的内容的寬/高,然後布局容器的寬/高 減去

去這幾個的寬/高和求出剩餘空間的寬/高,再按比例配置設定給每個View。

3.2.1 LinearLayout(線性布局)3.2.1 LinearLayout(線性布局)
3.2.1 LinearLayout(線性布局)3.2.1 LinearLayout(線性布局)
3.2.1 LinearLayout(線性布局)3.2.1 LinearLayout(線性布局)
3.2.1 LinearLayout(線性布局)3.2.1 LinearLayout(線性布局)

③match_parent

這個稍微好算一點,都設定成match_parent,weight設定為:1:2:3的,結果是這樣的

3.2.1 LinearLayout(線性布局)3.2.1 LinearLayout(線性布局)

換成1:2:2呢?

3.2.1 LinearLayout(線性布局)3.2.1 LinearLayout(線性布局)

接着我們來解釋下為什麼會出現這個結果,先說下1:2:3的

1:三個初始寬都是macth_parent,那麼剩餘的空間 = (1 - 3) * macth_parent = -2 * macth_parent

2:接着把剩餘的空間-2 * macth_parent分成6份(1+2+3),接着按比例配置設定:、

A的寬度 = macth_parent + (-2 * macth_parent /6 * 1) = 2/3 macth_parent;

B的寬度 = macth_parent + (-2 * macth_parent /6 * 2) = 1/3 macth_parent;

C的寬度 = macth_parent + (-2 * macth_parent /6 * 3) = 0 macth_parent;

非常簡單,你可以按照上面的計算方法接着試下1:2:2,得到的結果會是:3:1:1.

3.weightSum設定子控件weight之和

可以巧妙的用這個東西來實作留白,效果見示例代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="4">

    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:background="#03A9F4"
        android:gravity="center"
        android:text="A" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:background="#FFEB3B"
        android:gravity="center"
        android:text="B" />

</LinearLayout>
           
3.2.1 LinearLayout(線性布局)3.2.1 LinearLayout(線性布局)

4.為LinearLayout設定分割線

方法有兩種,第一種設定一個1px的View,第二種設定LinearLayout的divider相關的屬性

divider相關屬性:

  • divider設定作為分割線的圖檔,一般我們都會自己寫一個shape來作為分割線。
  • showDividers設定分割線的位置,none(無),begining(開始),end(結束),middle(每兩個元件間)
  • dividerPadding設定分割線的Padding

shape分割線模闆:

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
         android:shape="rectangle" >
     <size
         android:height="48dp"
         android:width="1dp" />
     <solid android:color="#000000" /> -->
 </shape>