【问题标题】:Android nested weightAndroid 嵌套权重
【发布时间】:2014-01-28 02:12:18
【问题描述】:

看起来在 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>

产生这个: 虽然它嵌套很重,而且嵌套的权重比父权重大得多。 LinearLayout 权重参数真的会阻止权重传播吗?这适用于所有 API 级别?

编辑

更清楚一点:我想使用嵌套权重而不损失性能,并且不使用其他布局,如 RelativeLayout,我认为这是一个解决方案,但我不太确定。

【问题讨论】:

  • 不确定我是否理解确切的问题。嵌套的权重不会阻止您实现效果。它只是警告您使用嵌套权重。
  • 我想停止将权重传播到嵌套/父布局,这是真实的,而不是 lint 警告。
  • 嵌套权重不利于性能。见this
  • @Tobor:我认为只有传播它才会对性能不利。在这个例子中,看起来第一个权重为 1.0 的红色视图没有与最后一个权重为 20000.0 的蓝色视图进行比较,因此,如果它不传播,它不会影响性能。

标签: android android-layout


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-05
    • 2012-04-22
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 2020-10-22
    • 2011-01-16
    相关资源
    最近更新 更多