嵌套重量警告

时间:2018-11-27 15:30:30

标签: android android-studio android-layout warnings

我在Android Studio中的布局上收到此警告:

  

嵌套权重不利于性能降低...
布局权重要求   一个要两次测量的小部件。当LinearLayout具有非零值时   权重嵌套在另一个具有非零权重的LinearLayout中,   然后测量的数量呈指数增长。
问题ID:   NestedWeights

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        style="?android:attr/buttonBarStyle">
        <Button
            android:id="@+id/b1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="#093A3E"
            android:foreground="?attr/selectableItemBackground"
            android:text="One"
            android:textAllCaps="false"
            android:textColor="#fff"
            android:textSize="18sp"
            style="?android:attr/buttonBarButtonStyle" />
        <Button
            android:id="@+id/b2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="#3AAFB9"
            android:foreground="?attr/selectableItemBackground"
            android:text="Two"
            android:textAllCaps="false"
            android:textColor="#fff"
            android:textSize="18sp"
            style="?android:attr/buttonBarButtonStyle" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        style="?android:attr/buttonBarStyle">
        <Button
            android:id="@+id/b6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:foreground="?attr/selectableItemBackground"
            android:layout_weight="1"
            android:background="#64E9EE"
            android:text="Three"
            android:textAllCaps="false"
            android:textColor="#fff"
            android:textSize="18sp"
            style="?android:attr/buttonBarButtonStyle" />
        <Button
            android:id="@+id/b7"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:foreground="?attr/selectableItemBackground"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="#97C8EB"
            android:text="Four"
            android:textAllCaps="false"
            android:textColor="#fff"
            android:textSize="18sp"
            style="?android:attr/buttonBarButtonStyle" />
    </LinearLayout>
</LinearLayout>

结果: enter image description here

如何避免出现此警告并获得相同的结果?谢谢。

1 个答案:

答案 0 :(得分:1)

为了获得更好的方法,我使用ConstraintLayout

发布了以下代码

为您的方法提供解决方案    从布局directory = r'../dir1/dir2/dir3'

中删除

您应该按照以下顺序重新格式化代码。

  1. LinearLayout(根)方向垂直(包含2个其他LinearLayouts)
  2. LinearLayout(顶部,内部根目录)水平方向(按钮权重为1)
  3. LinearLayout(底部,内部根)水平方向(按钮权重为1)

更好的方法是使用ConstraintLayout

enter image description here

android:layout_weight="1"