在这个计算器中哪个布局应该替换 LinearLayout
Posted
技术标签:
【中文标题】在这个计算器中哪个布局应该替换 LinearLayout【英文标题】:Which layout should replace LinearLayout in this calculator 【发布时间】:2021-05-24 19:37:40 【问题描述】:我已经使用 LinearLayout
实现了这个简单的计算器布局,但遇到了来自 android Studio 的“嵌套权重”警告。
这是我第一次尝试为 android 做一些事情,所以我开始阅读:TableLayout
和 GridLayout
会遇到与我理解的相同的问题,并且在任何地方都建议使用 ConstraintLayout
。
但我不知道如何使用ConstraintLayout
达到相同的结果。
可能吗?在这种情况下,放弃LinearLayout
是否值得?
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1"
android:paddingRight="0dp"
android:paddingTop="10dp"
android:orientation="vertical"
android:background="#F5F5F6"
>
<TextView
android:layout_
android:layout_
tools:text="2 + 2"
android:textSize="30sp"
android:gravity="right"
android:layout_marginRight="10dp"
/>
<TextView
android:layout_
android:layout_
tools:text="4"
android:textSize="30sp"
android:textColor="#000"
android:gravity="right"
android:layout_marginRight="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="2"
android:orientation="horizontal"
>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="3"
android:orientation="vertical"
>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1"
android:background="@color/colorAccent"
android:orientation="horizontal"
>
<Button
android:id="@+id/keyboard_num7"
android:layout_
android:layout_
android:layout_weight="1"
android:text="7"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num8"
android:layout_
android:layout_
android:layout_weight="1"
android:text="8"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num9"
android:layout_
android:layout_
android:layout_weight="1"
android:text="9"
style="@style/Operand"
/>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1"
android:background="@color/colorAccent"
android:orientation="horizontal"
>
<Button
android:id="@+id/keyboard_num4"
android:layout_
android:layout_
android:layout_weight="1"
android:text="4"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num5"
android:layout_
android:layout_
android:layout_weight="1"
android:text="5"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num6"
android:layout_
android:layout_
android:layout_weight="1"
android:text="6"
style="@style/Operand"
/>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1"
android:background="@color/colorAccent"
android:orientation="horizontal"
>
<Button
android:id="@+id/keyboard_num1"
android:layout_
android:layout_
android:layout_weight="1"
android:text="1"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num2"
android:layout_
android:layout_
android:layout_weight="1"
android:text="2"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num3"
android:layout_
android:layout_
android:layout_weight="1"
android:text="3"
style="@style/Operand"
/>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1"
android:background="@color/colorAccent"
android:orientation="horizontal"
>
<Button
android:id="@+id/keyboard_num_dot"
android:layout_
android:layout_
android:layout_weight="1"
android:text="."
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num0"
android:layout_
android:layout_
android:layout_weight="1"
android:text="0"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num_del"
android:layout_
android:layout_
android:layout_weight="1"
android:text="del"
style="@style/Operand"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1"
android:orientation="vertical"
android:background="#E1E2E1"
>
<Button
android:id="@+id/keyboard_division"
android:layout_
android:layout_
android:layout_weight="1"
android:text="÷"
style="@style/Operator"
/>
<Button
android:id="@+id/keyboard_multiplication"
android:layout_
android:layout_
android:layout_weight="1"
style="@style/Operator"
android:text="×"
/>
<Button
android:id="@+id/keyboard_minus"
android:layout_
android:layout_
android:layout_weight="1"
style="@style/Operator"
android:text="−"
/>
<Button
android:id="@+id/keyboard_plus"
android:layout_
android:layout_
android:layout_weight="1"
style="@style/Operator"
android:text="+"
/>
<Button
android:id="@+id/keyboard_equal"
android:layout_
android:layout_
android:layout_weight="1"
style="@style/Equal"
android:text="="
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
【问题讨论】:
您是否在 ConstraintLayout 中发现了阻止您执行此操作的任何内容? @MartinMarconcini 不,我缺乏知识阻止了我。现在我做到了(请参阅下面的答案),但仍有很多问题。 【参考方案1】:好吧,回答我自己的问题“如何使用ConstraintLayout
达到相同的结果”
我已经使用ConstraintLayout
使它变平了,但仍然不知道它是否值得,以及如何决定在可以使用任何布局时使用哪种布局。
从性能的角度来看,使用大小为 0 的 app:layout_constraintGuide_percent
和/或 chainStyle="spread_inside"
的准则在某种程度上比“嵌套权重”更好。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guidelineH"
android:layout_
android:layout_
app:layout_constraintGuide_percent="0.33"
android:orientation="horizontal"
/>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guidelineV"
android:layout_
android:layout_
app:layout_constraintGuide_percent="0.75"
android:orientation="vertical"
/>
<TextView
android:id="@+id/formula"
android:layout_
android:layout_
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/result"
app:layout_constraintVertical_chainStyle="packed"
tools:text="2+2"
android:textSize="36sp"
android:gravity="right"
android:layout_marginRight="10dp"
/>
<TextView
android:id="@+id/result"
android:layout_
android:layout_
app:layout_constraintTop_toBottomOf="@id/formula"
app:layout_constraintBottom_toTopOf="@id/guidelineH"
app:layout_constraintVertical_chainStyle="packed"
tools:text="4"
android:textSize="30sp"
android:textColor="#000"
android:gravity="right"
android:layout_marginRight="10dp"
/>
<Button
android:id="@+id/keyboard_num7"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_num4"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/keyboard_num8"
app:layout_constraintTop_toBottomOf="@id/guidelineH"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="7"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num8"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_num5"
app:layout_constraintLeft_toRightOf="@id/keyboard_num7"
app:layout_constraintRight_toLeftOf="@id/keyboard_num9"
app:layout_constraintTop_toBottomOf="@id/guidelineH"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="8"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num9"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_num6"
app:layout_constraintLeft_toRightOf="@id/keyboard_num8"
app:layout_constraintRight_toLeftOf="@id/guidelineV"
app:layout_constraintTop_toBottomOf="@id/guidelineH"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="9"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num4"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_num1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/keyboard_num5"
app:layout_constraintTop_toBottomOf="@id/keyboard_num7"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="4"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num5"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_num2"
app:layout_constraintLeft_toRightOf="@id/keyboard_num4"
app:layout_constraintRight_toLeftOf="@id/keyboard_num6"
app:layout_constraintTop_toBottomOf="@id/keyboard_num8"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="5"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num6"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_num3"
app:layout_constraintLeft_toRightOf="@id/keyboard_num5"
app:layout_constraintRight_toLeftOf="@id/guidelineV"
app:layout_constraintTop_toBottomOf="@id/keyboard_num9"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="6"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num1"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_num_dot"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/keyboard_num2"
app:layout_constraintTop_toBottomOf="@id/keyboard_num4"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="1"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num2"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_num0"
app:layout_constraintLeft_toRightOf="@id/keyboard_num1"
app:layout_constraintRight_toLeftOf="@id/keyboard_num3"
app:layout_constraintTop_toBottomOf="@id/keyboard_num5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="2"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num3"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_num_del"
app:layout_constraintLeft_toRightOf="@id/keyboard_num2"
app:layout_constraintRight_toLeftOf="@id/guidelineV"
app:layout_constraintTop_toBottomOf="@id/keyboard_num6"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="3"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num_dot"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/keyboard_num0"
app:layout_constraintTop_toBottomOf="@id/keyboard_num1"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="."
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num0"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/keyboard_num_dot"
app:layout_constraintRight_toLeftOf="@id/keyboard_num_del"
app:layout_constraintTop_toBottomOf="@id/keyboard_num2"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="0"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num_del"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/keyboard_num0"
app:layout_constraintRight_toLeftOf="@id/guidelineV"
app:layout_constraintTop_toBottomOf="@id/keyboard_num3"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="del"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_division"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_multiplication"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/guidelineH"
app:layout_constraintLeft_toRightOf="@id/guidelineV"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
android:text="÷"
style="@style/Operator"
/>
<Button
android:id="@+id/keyboard_multiplication"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_minus"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/keyboard_division"
app:layout_constraintLeft_toRightOf="@id/guidelineV"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
style="@style/Operator"
android:text="×"
/>
<Button
android:id="@+id/keyboard_minus"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_plus"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/keyboard_multiplication"
app:layout_constraintLeft_toRightOf="@id/guidelineV"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
style="@style/Operator"
android:text="−"
/>
<Button
android:id="@+id/keyboard_plus"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@id/keyboard_equal"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/keyboard_minus"
app:layout_constraintLeft_toRightOf="@id/guidelineV"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
style="@style/Operator"
android:text="+"
/>
<Button
android:id="@+id/keyboard_equal"
android:layout_
android:layout_
app:layout_constraintTop_toBottomOf="@id/keyboard_plus"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@id/guidelineV"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
style="@style/Equal"
android:text="="
/>
</androidx.constraintlayout.widget.ConstraintLayout>
【讨论】:
几个cmets: 1.记住不要在任何地方使用left/right,用start/End替换它们。 (除非您针对 API 16 链/指导线是否比嵌套权重更好?是的。 Android 显示布局的时间很大程度上受布局深度(嵌套的东西)的影响,因为所有这些最终都是 List以上是关于在这个计算器中哪个布局应该替换 LinearLayout的主要内容,如果未能解决你的问题,请参考以下文章