嵌套权重警告
Nested weights warning
我在 Android Studio 中的布局上收到此警告:
Nested weights are bad for performance less...
Layout weights require
a widget to be measured twice. When a LinearLayout with non-zero
weights is nested inside another LinearLayout with non-zero weights,
then the number of measurements increase exponentially.
Issue 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>
结果:
如何避免此警告并获得相同的结果?谢谢
为了更好的方法,我使用 ConstraintLayout
发布了下面的代码
对于您的方法,这里是解决方案
从布局中删除 android:layout_weight="1"
您应该按照以下顺序重新格式化您的代码。
- LinearLayout(根)方向垂直(包含 2 个其他 LinearLayout)
- LinearLayout(顶部,内部根)方向水平(按钮权重为 1)
- LinearLayout(底部,内部根)水平方向(按钮权重为 1)
更好的方法是使用 ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="1"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button4"
app:layout_constraintTop_toBottomOf="@+id/button3" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="2"
app:layout_constraintBottom_toTopOf="@+id/button5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
我在 Android Studio 中的布局上收到此警告:
Nested weights are bad for performance less...
Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.
Issue 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>
结果:
如何避免此警告并获得相同的结果?谢谢
为了更好的方法,我使用 ConstraintLayout
发布了下面的代码对于您的方法,这里是解决方案
从布局中删除 android:layout_weight="1"
您应该按照以下顺序重新格式化您的代码。
- LinearLayout(根)方向垂直(包含 2 个其他 LinearLayout)
- LinearLayout(顶部,内部根)方向水平(按钮权重为 1)
- LinearLayout(底部,内部根)水平方向(按钮权重为 1)
更好的方法是使用 ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="1"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button4"
app:layout_constraintTop_toBottomOf="@+id/button3" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="2"
app:layout_constraintBottom_toTopOf="@+id/button5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>