2012-03-30 104 views
0

在相機應用的佈局有:需要幫助理解layout_weight = 「1」

camera.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/app_root" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<include layout="@layout/preview_frame"/> 
<include layout="@layout/camera_control"/> 
</LinearLayout> 

和preview_frame.xml是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/frame_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 
    ..... 
</RelativeLayout> 

和camera_control。 xml是:

<ControlPanelLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/control_panel" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="76dp" 
    android:background="@drawable/bg_camera_pattern"> 
    ..... 
</ControlPanelLayout> 

我的問題是preview_frame.xml中的'layout_weight =「1」'是什麼? 我已閱讀關於layout_weight,但大部分時間它與layout_width =「0px」 一起使用而在camera_control中,它沒有指定任何layout_weight。

但在這種情況下,它不是。

謝謝。

回答

1

佈局權重決定了在所有添加的視圖佔用其基本區域後父區中的任何剩餘空間的填充情況。

將視圖組中的視圖權重相加,剩餘空間按比例分配。因此,兩個寬度爲0和權重爲1和2的視圖總和爲3的權重,一個視圖將擴展到視圖大小的1/3,另一個視圖的大小爲2/3。

但是,如果視圖有寬度,它們將在分配額外空間之前佔用該空間。因此,寬度和無重量的視圖將佔用分配的空間,旁邊的加權視圖將填充剩餘空間,因此在我們前面的權重1和2的示例中,如果重量1視圖具有寬度,則它將擴展到另外的包括剩餘空間的1/3,可能會使其大於重量2視圖。

有點不專心,沒有給我咖啡,但我希望它可以幫助你:P

0

佈局重量是各種佈局元素的相對權重。 如果一個元素的權重爲1,其他元素的權重爲0,則第一個元素將佔用所有可用空間,除了其他元素需要的最小值。 如果一個元素的權重爲1,另一個權重爲2,則第一個元素佔1/3的空間,另一個爲2/3。

此致敬禮。