首页 > 解决方案 > 布局不适用于所有 android 设备的屏幕尺寸

问题描述

我有以下布局。我有这个布局,但它不适合所有设备。我遇到的问题是我的布局不适合每个页面,在较小的手机上它们会离开屏幕框,尽管我为必要的布局选择了匹配父选项。你能帮我解决这个问题?非常感谢。

设计 输出

         <?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>

标签: androidxmlandroid-layoutlayout

解决方案


这个怎么样?我使用与 RelativeLayout 的组合来获取简码并修复 android 身高和体重问题

尺寸 6.0

尺寸 6.0

尺寸 5.0

尺寸 5.0

<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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="visible"
        android:layout_alignParentBottom="true"
        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="wrap_content"
            android:layout_weight="40"
            android:background="@color/white"
            android:text="Result"
            android:gravity="bottom|end"
            android:maxLength="10"
            android:padding="8dp"
            android:textSize="36sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            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: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: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: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: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: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: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: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: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: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:text="="
                    android:textSize="20sp" />
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>
    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

推荐阅读