简体   繁体   English

显示大小正在影响我的应用程序布局 Android?

[英]Display size is affecting my app layout Android?

So I have been building a simple login screen and the display size of the device is affecting the layout and it keeps changing on different devices, New to the android studio is there a solution?所以我一直在构建一个简单的登录屏幕,设备的显示尺寸正在影响布局,并且在不同的设备上不断变化,android 工作室的新手有解决方案吗?

显示下的布局 - 小

显示下的布局 - 大

显示下的布局 - Xlarge

XML code XML代码

its a simple layout with Neumorphism.它是一个带有 Neumorphism 的简单布局。

**And I know we can make the layout responsive with constraint but is there any other way other than constraint layout? **而且我知道我们可以使布局响应约束,但是除了约束布局之外还有其他方法吗? ** **

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

So the problem is you are giving bad constraint that wise it differs in all device.所以问题是你给出了不好的约束,明智地它在所有设备中都不同。 I highly suggest you to read this article so it will improve your constraint things.我强烈建议你阅读这篇文章,这样它会改善你的约束条件。 enter link description here 在此处输入链接描述

Because every device has different ratios, so the controls may get their position changed in different devices.由于每个设备的比率不同,因此控件可能会在不同设备中更改其 position。 So for this purpose you should use Constraint Layout.所以为了这个目的,你应该使用约束布局。 Set the constraints and now your application should work.设置约束,现在您的应用程序应该可以工作了。

It is true that using constraint layout is time consuming.确实,使用约束布局非常耗时。 But it is the best option to maintain the position of controls但最好的选择是保持控件的 position

Otherwise use Linear Layout.否则使用线性布局。

No need to worry about dimensions.无需担心尺寸。

An android lib that provides size unit - dp and sp.一个 android 库,提供大小单位 - dp 和 sp。 This size unit scales with the screen size and font size.该尺寸单位随屏幕尺寸和字体大小而变化。 It can help Android developers with supporting multiple screens.它可以帮助Android开发者支持多屏。

or或者

Simply use this dependency只需使用此依赖项

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

and then use ssp instance of sp and sdp instance of dp然后使用 sp 的 ssp 实例和 dp 的 sdp 实例

refer from参考自

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

Try using ConstraintLayout for this purpose.尝试为此目的使用 ConstraintLayout。 It works well for me.这对我来说很有用。 Just change your layout to ConstraintLayout and set all the constraints according to your needs.只需将您的布局更改为 ConstraintLayout 并根据您的需要设置所有约束。 Hope this works.希望这有效。

If you need to create responsive layout here is some options如果您需要创建响应式布局,这里有一些选项

  • Use Constaint Layout使用约束布局
  • build your layout with wrap_content and match_parent as possible尽可能使用wrap_contentmatch_parent构建布局
  • Try out layout_weight when you are using linear layout使用线性布局时试试layout_weight

Here is some resource to create responsive layout这是创建响应式布局的一些资源

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

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

Hope you helpful!希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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