【发布时间】: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_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingRight="0dp"
android:paddingTop="10dp"
android:orientation="vertical"
android:background="#F5F5F6"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="2 + 2"
android:textSize="30sp"
android:gravity="right"
android:layout_marginRight="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="4"
android:textSize="30sp"
android:textColor="#000"
android:gravity="right"
android:layout_marginRight="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/colorAccent"
android:orientation="horizontal"
>
<Button
android:id="@+id/keyboard_num7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9"
style="@style/Operand"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/colorAccent"
android:orientation="horizontal"
>
<Button
android:id="@+id/keyboard_num4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"
style="@style/Operand"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/colorAccent"
android:orientation="horizontal"
>
<Button
android:id="@+id/keyboard_num1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"
style="@style/Operand"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/colorAccent"
android:orientation="horizontal"
>
<Button
android:id="@+id/keyboard_num_dot"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="."
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"
style="@style/Operand"
/>
<Button
android:id="@+id/keyboard_num_del"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="del"
style="@style/Operand"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:background="#E1E2E1"
>
<Button
android:id="@+id/keyboard_division"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="÷"
style="@style/Operator"
/>
<Button
android:id="@+id/keyboard_multiplication"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/Operator"
android:text="×"
/>
<Button
android:id="@+id/keyboard_minus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/Operator"
android:text="−"
/>
<Button
android:id="@+id/keyboard_plus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/Operator"
android:text="+"
/>
<Button
android:id="@+id/keyboard_equal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/Equal"
android:text="="
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
【问题讨论】:
-
您是否在 ConstraintLayout 中发现了阻止您执行此操作的任何内容?
-
@MartinMarconcini 不,我缺乏知识阻止了我。现在我做到了(请参阅下面的答案),但仍有很多问题。
标签: android android-layout android-linearlayout android-constraintlayout