【发布时间】:2021-12-15 08:58:02
【问题描述】:
我有以下布局。我有这个布局,但它不适合所有设备。我遇到的问题是我的布局不适合每个页面,并且在较小的手机上它们会离开屏幕框,尽管我为必要的布局选择了匹配父选项。你能帮我解决这个问题? 非常感谢。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textViewInput"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:background="@color/white"
android:gravity="bottom|end"
android:maxLength="10"
android:padding="8dp"
android:textSize="36sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="60"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonDigit7"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onDigitClicked"
android:text="7"
android:textSize="20sp" />
<Button
android:id="@+id/buttonDigit8"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onDigitClicked"
android:text="8"
android:textSize="20sp" />
<Button
android:id="@+id/buttonDigit9"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onDigitClicked"
android:text="9"
android:textSize="20sp" />
<Button
android:id="@+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onOperatorClick"
android:text="/"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonDigit4"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onDigitClicked"
android:text="4"
android:textSize="20sp" />
<Button
android:id="@+id/buttonDigit5"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onDigitClicked"
android:text="5"
android:textSize="20sp" />
<Button
android:id="@+id/buttonDigit6"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onDigitClicked"
android:text="6"
android:textSize="20sp" />
<Button
android:id="@+id/buttonMultiple"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onOperatorClick"
android:text="*"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonDigit1"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onDigitClicked"
android:text="1"
android:textSize="20sp" />
<Button
android:id="@+id/buttonDigit2"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onDigitClicked"
android:text="2"
android:textSize="20sp" />
<Button
android:id="@+id/buttonDigit3"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onDigitClicked"
android:text="3"
android:textSize="20sp" />
<Button
android:id="@+id/buttonMinus"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onOperatorClick"
android:text="-"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonDot"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onDecimalPointClick"
android:text="."
android:textSize="20sp" />
<Button
android:id="@+id/buttonDigit0"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onDigitClicked"
android:text="0"
android:textSize="20sp" />
<Button
android:id="@+id/buttonClear"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="clearText"
android:text="CLR"
android:textSize="20sp" />
<Button
android:id="@+id/buttonPlus"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:onClick="onOperatorClick"
android:text="+"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonEqual"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="3dp"
android:backgroundTint="@color/black"
android:onClick="onEqualClick"
android:text="="
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
-
如果你只想在其中使用线性布局,那么使用约束布局有什么意义?
-
Match_parent 不是一个神奇的“让我的设计适合”按钮。 Match_parent 意味着它应该和父级一样大。如果您的布局不适合,它会超出父级,并且不会显示该部分。使其适用于所有屏幕尺寸仍然需要您努力工作并创建适合全球的设计。
标签: android xml android-layout layout