显示尺寸影响我的应用程序布局 Android?

Display size is affecting my app layout Android?

所以我一直在构建一个简单的登录屏幕,设备的显示尺寸会影响布局,并且它会在不同的设备上不断变化,android studio 的新手有解决方案吗?

XML代码

这是一个简单的 Neumorphism 布局。

**我知道我们可以让布局响应约束但除了约束布局还有其他方法吗? **

<ScrollView 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:orientation="vertical"
tools:context=".Admin.AdminStuff.SettingUp.admin_log_in">

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


    <ImageView
        android:id="@+id/backButton_students"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/arrow_back"
        app:tint="@color/black" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:fontFamily="@font/muli"
        android:text="SUMS"
        android:textColor="@color/appcolor"
        android:textSize="60sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="2dp"
        android:fontFamily="sans-serif-medium"
        android:text="Log In"
        android:textColor="@color/black"
        android:textSize="28sp"
        android:textStyle="bold" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="20dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="-4dp"
            android:layout_marginTop="50dp"
            android:layout_marginBottom="-5dp"
            android:text="Please select the Institution &amp; Class"
            android:textAlignment="center"
            android:textColor="@color/darkblue"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/spinner_students_login"
            style="@style/Widget.AppCompat.DropDownItem.Spinner"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginStart="30dp"
            android:layout_marginTop="15dp"
            android:layout_marginEnd="30dp"
            android:background="#f5f7fa"
            android:textAlignment="center" />

        <Spinner
            android:id="@+id/spinner_programmes_students_login"
            style="@style/Widget.AppCompat.DropDownItem.Spinner"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginStart="30dp"
            android:layout_marginTop="15dp"
            android:layout_marginEnd="30dp"
            android:background="#f5f7fa"
            android:textAlignment="center" />


        <soup.neumorphism.NeumorphCardView
            style="@style/Widget.Neumorph.CardView"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_marginStart="15dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="15dp"
            android:layout_marginBottom="5dp"
            app:neumorph_shapeType="pressed">


            <EditText
                android:id="@+id/registrationNumber_studentslogin"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/no_background"
                android:hint="Email Id"
                android:inputType="textEmailAddress"
                android:paddingStart="15dp"
                android:textCursorDrawable="@drawable/cursor" />


        </soup.neumorphism.NeumorphCardView>

        <soup.neumorphism.NeumorphCardView
            style="@style/Widget.Neumorph.CardView"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            app:neumorph_shapeType="pressed">


            <EditText
                android:id="@+id/password_studentslogin"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/no_background"
                android:hint="Password"
                android:inputType="textVisiblePassword"
                android:maxLines="1"
                android:paddingStart="15dp"
                android:textCursorDrawable="@drawable/cursor" />


        </soup.neumorphism.NeumorphCardView>


    </LinearLayout>

    <soup.neumorphism.NeumorphButton
        android:id="@+id/forgotpassword_students"
        style="@style/Widget.Neumorph.Button"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_gravity="right"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="-25dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:backgroundTint="@color/white"
        android:text="Forgot Password?"
        android:textColor="#636363"
        android:textStyle="bold"
        app:neumorph_lightSource="leftTop"
        app:neumorph_shadowColorDark="@color/white" />


    <CheckBox
        android:id="@+id/remeberme_student"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/login_teachers"
        android:layout_marginStart="25dp"
        android:layout_marginTop="-60dp"
        android:layout_marginBottom="25dp"
        android:text="Remember Me"
        android:textColor="@color/black"
        android:textSize="16sp" />


    <soup.neumorphism.NeumorphButton
        android:id="@+id/login_students"
        style="@style/Widget.Neumorph.Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="25dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="10dp"
        android:backgroundTint="@color/appcolor"
        android:text="Log In"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold"
        app:cornerRadius="4dp"
        app:neumorph_backgroundColor="@color/appcolor" />

    <ProgressBar
        android:id="@+id/progressbar_students"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:outlineSpotShadowColor="@color/white" />

    <soup.neumorphism.NeumorphButton
        android:id="@+id/adminNewUser_student"
        style="@style/Widget.Neumorph.Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="-15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="10dp"
        android:backgroundTint="@color/white"
        android:shadowDx="-2"
        android:shadowDy="-2"
        android:shadowRadius="1"
        android:text="New User? Sign Up"
        android:textColor="@color/darkblue"
        android:textSize="18sp"
        android:textStyle="bold"
        app:cornerRadius="4dp" />

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginTop="80dp"
        android:src="@drawable/bottom" />


</LinearLayout>

为此尝试使用 ConstraintLayout。这对我来说很有用。只需将您的布局更改为 ConstraintLayout 并根据您的需要设置所有约束。希望这有效。

如果您需要创建响应式布局,这里有一些选项

  • 使用约束布局
  • 尽可能使用 wrap_contentmatch_parent 构建布局
  • 在使用线性布局时尝试 layout_weight

这是创建响应式布局的一些资源

  1. https://developer.android.com/training/constraint-layout

  2. https://developer.android.com/training/constraint-layout

希望对您有所帮助!

所以问题是你给出了错误的约束,因为它在所有设备上都不同。 我强烈建议您阅读这篇文章,它会改善您的约束条件。 enter link description here

由于每个设备都有不同的比例,所以控件的位置可能会在不同的设备上发生变化。 因此,为此目的,您应该使用约束布局。设置约束,现在您的应用程序应该可以运行了。

确实,使用约束布局很耗时。但保持控件位置是最好的选择

否则使用线性布局。

无需担心尺寸。

android 提供大小单位的库 - dp 和 sp。此大小单位与屏幕大小和字体大小成比例。它可以帮助 Android 开发人员支持多屏幕。

简单的使用这个依赖

实施'com.intuit.ssp:ssp-android:1.0.6' 实施 'com.intuit.sdp:sdp-android:1.0.6'

然后使用sp的ssp实例和dp的sdp实例

参考自

https://github.com/intuit/ssp https://github.com/intuit/sdp