1. 程式人生 > >Android小技巧——LinearLayout巧妙的平均分配空間

Android小技巧——LinearLayout巧妙的平均分配空間

當我們編寫Android UI的時候,肯定會遇到這樣的UI設計,在螢幕寬度裡面線性橫向排列有三個View,每個View平分螢幕寬度。乍一看,這個很簡單嘛,給這三個View都設定一個相同的width就好嘛,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:background
="@android:color/white" android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:text="AAAAAAAA" android:layout_width="100dp" android:layout_height="wrap_content"/> <Button android:text="BBBBBBBB"
android:layout_width="100dp" android:layout_height="wrap_content"/>
<Button android:text="CCCC" android:layout_width="100dp" android:layout_height="wrap_content"/> </LinearLayout>

但問題是它們的寬度是不確定的,有可能動態的變化,比如說它的父佈局可能不是螢幕寬度等等。所以這個方法是下下策。也許有人會說,這個也不難嗎,我在java程式碼裡面動態的去設定它們的寬度相同就好了。但這個的前提是需要先獲取它的父佈局裡的寬度,而且噹噹activity生命週期執行到onresume的時候,整個當前頁面UI其實還沒有真正的繪製到window中,這也是我們經常在生命週期中獲取到某個view的width、height為0的原因,考慮到這個,開發者往往還要藉助View.getViewTreeObserver().addOnGlobalLayoutListener的佈局監聽來解決,既然add了,那就有remove操作等,然而回到我們的問題,我不過是要view平均分配空間而已,真的需要新增如此多的程式碼?

另外有一些人會說,我使用LiearLayout,設定三個view的layout_weight屬性都相同不就可以了嗎?例如下面程式碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:background="@android:color/white"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <Button
        android:layout_weight="1"
        android:text="aaa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_weight="1"
        android:text="bbb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_weight="1"
        android:text="ccc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

這裡寫圖片描述

看上去,上面的方法好像可以解決問題,然而如果每個子view的內容寬度不一樣的時候也能保持平均分配嗎?答案當然是NO!例如下面程式碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:background="@android:color/white"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <Button
        android:layout_weight="1"
        android:text="aaaaaaaaaaaaaaa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_weight="1"
        android:text="bbbbb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_weight="1"
        android:text="c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

這裡寫圖片描述

所以通過設定相同的layout_weight只能解決每個子view內容寬度都相同的情況下的問題。

現在說一個非常實用的小技巧:
在LinearLayout佈局中,巧妙的設定width/height和layout_weight屬性就能達到子view平均分配父view空間的效果
例子程式碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:background="@android:color/white"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <Button
        android:layout_weight="1"
        android:text="aaaaaaaaaaaa"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_weight="1"
        android:text="bbbbb"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_weight="1"
        android:text="c"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>
</LinearLayout>

這裡寫圖片描述

必須要注意的是:務必將width和layout_weight搭配使用,否則程式碼會報錯

好了,上面說的是平分父view width的例子,平分父view height的方式也是一樣的,此處就不贅述了。