Android嵌套重量

时间:2014-01-27 17:33:13

标签: android android-layout

看起来在Android的LinearLayout上避免嵌套权重的一种方法是为嵌套的LinearLayout设置权重参数。例如,这个布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<View
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.0"
    android:background="#FF0000" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2.0" >

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="10.0"
        android:background="#00FF00" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20.0"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="100.0"
            android:background="#0000FF" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="200.0" >

            <View
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1000.0"
                android:background="#FF0000" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2000.0"
                android:orientation="vertical" >

                <View
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="10000.0"
                    android:background="#00FF00" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="20000.0"
                    android:background="#0000FF" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

产生这个: enter image description here 虽然它是嵌套的,嵌套权重远大于父权重。 LinearLayout权重参数是否真的阻止了权重传播?这适用于所有API级别?

修改

更清楚:我想使用嵌套权重而不会丢失性能而不使用像RelativeLayout这样的其他布局,我认为这是一个解决方案,但我不太确定。

1 个答案:

答案 0 :(得分:0)

线性布局权重仅适用于特定布局的子项。没有重量值“传播” - 重要的是儿童的实际大小。

每个加权线性布局首先像往常一样测量子项,然后执行附加度量/布局传递,将线性布局中的任何剩余空间分配给比例中的子项他们的重量。如果所有权重都为零(非权重布局中的情况),则不需要此步骤。

为什么嵌套权重对于性能不利,每个嵌套级别都会使度量/布局次数增加一倍。例如,在具有4个加权线性布局的示例中,将有2 ^ 4 = 16个度量/布局过程。

相关问题