简体   繁体   中英

Layout is not working on all android devices screen size

I have the following layout. I have this layout but it does not fit of all devices.I had the problem that my layouts do not fit on every page and on smaller phones they leave the screen box, although I chose the match parent option for the necessary layouts.Can you help me to solve this problem? Thanks a lot.

design output

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

How about this one? I use a combination with RelativeLayout to get shortcode and fix android height and weight issues

size 6.0

尺寸 6.0

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM