【问题标题】:Display size is affecting my app layout Android?显示大小正在影响我的应用布局 Android?
【发布时间】:2021-08-05 10:00:32
【问题描述】:

所以我一直在构建一个简单的登录屏幕,设备的显示尺寸正在影响布局,并且在不同的设备上不断变化,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>

【问题讨论】:

  • is there a solution ? 当然。利用较小的尺寸,使用约束布局之类的东西,练习更多的布局。如果您不提供代码,则无法解决
  • 是的,约束布局使屏幕响应。但除此之外还有其他方法吗,因为约束布局太痛苦了。

标签: android xml android-layout layout


【解决方案1】:

无需担心尺寸。

提供大小单位的 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

【讨论】:

  • 我昨天才发现这个,它很好用。谢谢
【解决方案2】:

所以问题是你给出了不好的约束,明智地它在所有设备中都不同。 我强烈建议你阅读这篇文章,这样它会改善你的约束条件。 enter link description here

【讨论】:

  • 是的,约束布局使屏幕响应。但除此之外还有其他方法吗,因为约束布局太痛苦了。
  • 您可以使用其他布局,例如线性布局、相对布局,但如果您问我约束布局是最好的。半个月前我遇到了同样的问题,然后我找到了这篇文章,它解决了我的问题。我强烈建议你阅读那篇文章,你一定会解决你的问题。
【解决方案3】:

因为每个设备都有不同的比率,所以控件可能会在不同的设备中改变它们的位置。 所以为了这个目的,你应该使用约束布局。设置约束,现在您的应用程序应该可以工作了。

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

否则使用线性布局。

【讨论】:

    【解决方案4】:

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

    【讨论】:

    • 是的,约束布局使屏幕响应。但除此之外还有其他方法吗,因为约束布局太痛苦了。
    【解决方案5】:

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

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

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

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

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

    希望对您有所帮助!

    【讨论】:

    • 是的,约束布局使屏幕响应。但除此之外还有其他方法吗,因为约束布局太痛苦了。
    • 使用线性布局
    猜你喜欢
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 2014-01-15
    • 2020-02-23
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    相关资源
    最近更新 更多