Android布局自动调整到不同的屏幕大小

i2byvkas  于 2023-02-06  发布在  Android
关注(0)|答案(2)|浏览(147)

我在下面的xml中嵌套了线性布局并分配了weightSum,但是在不同的屏幕尺寸下,很少有editText相互重叠或移出屏幕。我尝试更改weightSum的值并添加layout_weight,但是我的xml不兼容不同的屏幕尺寸。我如何使它们兼容所有的屏幕尺寸?有什么建议吗?

<RelativeLayout 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"
android:background="#5BBEE7ED"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="20dp"
    android:layout_marginRight="15dp"
    android:layout_marginBottom="60dp"
    android:background="@drawable/card_background"
    android:elevation="10dp"
    android:orientation="vertical"
    android:weightSum="4">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="2">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtPond"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Pond / Tank Size"
                android:textColor="@color/black"
                android:textSize="18sp" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtDensity"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtPond"
                android:layout_centerHorizontal="true"
                android:hint="Stocking Density (m2)"
                android:textColor="@color/black"
                android:textSize="18sp" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtShrimp"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtDensity"
                android:layout_centerHorizontal="true"
                android:hint="Number of Shrimp"
                android:textColor="@color/black"
                android:textSize="18sp" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtBodyWt"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtShrimp"
                android:layout_centerHorizontal="true"
                android:hint="Average Body Weight"
                android:textColor="@color/black"
                android:textSize="18sp" />
        </com.google.android.material.textfield.TextInputLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtOxy"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="395dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtBodyWt"
                android:layout_centerHorizontal="true"
                android:hint="Oxygen"
                android:textColor="@color/black"
                android:textSize="18sp" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtTemp"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="395dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtOxy"
                android:layout_centerHorizontal="true"
                android:hint="Temperature"
                android:textColor="@color/black"
                android:textSize="18sp" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtPh"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="395dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtTemp"
                android:layout_centerHorizontal="true"
                android:hint="Ph"
                android:textColor="@color/black"
                android:textSize="18sp"
                app:boxCornerRadiusBottomEnd="13dp"
                app:boxCornerRadiusBottomStart="13dp"
                app:boxCornerRadiusTopEnd="13dp"
                app:boxCornerRadiusTopStart="13dp" />
        </com.google.android.material.textfield.TextInputLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnSubmit"
            style="@style/Widget.MaterialComponents.Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Submit" />
    </LinearLayout>

</LinearLayout>
aydmsdu9

aydmsdu91#

使用库https://github.com/intuit/ssphttps://github.com/intuit/sdp
将其添加到依赖项实现“com.intuit.sdp:sdp-android:1.1.0”实现“com.intuit.ssp:ssp-android:1.1.0”中

wgx48brx

wgx48brx2#

<RelativeLayout 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"
android:background="#5BBEE7ED"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/ll_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="20dp"
    android:layout_marginRight="15dp"
    android:background="@color/white"
    android:orientation="vertical">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtPond"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Pond / Tank Size"
                android:textColor="@color/black"
                android:textSize="18sp" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtDensity"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtPond"
                android:layout_centerHorizontal="true"
                android:hint="Stocking Density (m2)"
                android:textColor="@color/black"
                android:textSize="18sp" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtShrimp"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtDensity"
                android:layout_centerHorizontal="true"
                android:hint="Number of Shrimp"
                android:textColor="@color/black"
                android:textSize="18sp" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtBodyWt"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtShrimp"
                android:layout_centerHorizontal="true"
                android:hint="Average Body Weight"
                android:textColor="@color/black"
                android:textSize="18sp" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtOxy"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_below="@+id/edtBodyWt"
                android:hint="Oxygen"
                android:textColor="@color/black"
                android:textSize="18sp"/>
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtTemp"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            app:boxCornerRadiusBottomEnd="13dp"
            app:boxCornerRadiusBottomStart="13dp"
            app:boxCornerRadiusTopEnd="13dp"
            app:boxCornerRadiusTopStart="13dp"
            app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtOxy"
                android:layout_centerHorizontal="true"
                android:hint="Temperature"
                android:textColor="@color/black"
                android:textSize="18sp" />
        </com.google.android.material.textfield.TextInputLayout>

       <com.google.android.material.textfield.TextInputLayout
           android:id="@+id/edtPh"
           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:padding="10dp"
           app:boxCornerRadiusBottomEnd="13dp"
           app:boxCornerRadiusBottomStart="13dp"
           app:boxCornerRadiusTopEnd="13dp"
           app:boxCornerRadiusTopStart="13dp"
           app:endIconMode="clear_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtTemp"
                android:layout_centerHorizontal="true"
                android:hint="Ph"
                android:textColor="@color/black"
                android:textSize="18sp" />

        </com.google.android.material.textfield.TextInputLayout>

        <!--Button-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnSubmit"
                style="@style/Widget.MaterialComponents.Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Submit" />
        </LinearLayout>

    </LinearLayout>

尝试使用sdp/ssp库而不是dp

相关问题