我是否需要更改xml代码以支持所有屏幕尺寸的android设备? [英] Do I need to change my xml code for supporting all screen size android devices?

查看:65
本文介绍了我是否需要更改xml代码以支持所有屏幕尺寸的android设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为不同的屏幕尺寸创建了布局,这是我的xml代码,用于默认布局 但请任何人告诉我,对于任何屏幕尺寸的android设备,我该如何支持我的布局.

I have created layout for different screen size and this is my xml code for default layout but so any one please tell me what I should do to support my layout for any screen size android devices.

我需要更改我的xml代码吗? 或者 只是我需要修改我的xml代码?

do I need to change my xml code? or just I need to modify my xml code ?

请告诉我,以便我解决问题

please tell me so I can solve my problem

xlm

xlm

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Login"
            android:textSize="40sp"
            android:layout_marginTop="40dp"
            android:fontFamily="sans-serif-black"
            android:layout_gravity="center"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/nice"
            android:layout_marginTop="40dp"
            android:textSize="30sp"/>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayoutUsername"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:errorEnabled="true"
            app:errorTextAppearance="@style/ErrorStyleNormal"
            android:padding="5dp"
            android:layout_marginTop="30dp"
            app:hintTextAppearance="@style/ErrorStyleNormal">
            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="25sp"
                android:hint="Username"
                />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayoutPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            app:errorEnabled="true"
            app:passwordToggleEnabled="true"
            app:errorTextAppearance="@style/ErrorStyleNormal"
            android:layout_marginTop="20dp"
            app:hintTextAppearance="@style/ErrorStyleNormal"
            app:counterTextAppearance="@style/CounterStyleNormal">
            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:textSize="25sp"
                android:inputType="textPassword"/>
        </com.google.android.material.textfield.TextInputLayout>

        <TextView
            android:id="@+id/textViewForgotPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_marginEnd="15dp"
            android:layout_marginRight="15dp"
            android:text="@string/ForgotPassword"
            android:textSize="15sp" />

        <androidx.cardview.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:cardCornerRadius="25dp"
            android:elevation="5dp"
            android:padding="10dp"
            android:layout_marginTop="30dp">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="5dp">
                <ImageView
                    android:id="@+id/image"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:src="@drawable/ic_arrow_forward"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/Login"
                    android:layout_toEndOf="@+id/image"
                    android:layout_centerVertical="true"
                    android:textSize="25sp"
                    android:layout_marginRight="10dp"
                    android:layout_marginEnd="10dp"
                    android:fontFamily="sans-serif-black"
                    android:layout_toRightOf="@+id/image" />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="@string/To_Join_With"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:textColor="@android:color/black"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="@string/Click_here"
            android:layout_gravity="center"
            android:textColor="@android:color/holo_blue_bright"/>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

推荐答案

我编辑了XML,因此它可能是学习如何使用ConstraintLayout的一个很好的开始.查看有关ConstraintLayout(constraintlayout.com)的教程

I edited you XML so it may be a good start for learning how to use ConstraintLayout. Check a tutorial for ConstraintLayout (constraintlayout.com)

  1. 不要将match_parent与ConstraintLayout一起使用,我们只有0dp(match_constraint)和wrap_conent
  2. 不要用不必要的嵌套ViewGroup(LinearLayout)弄乱XML
  3. 使用边距或填充时最好使用基于8的数字(8-16-24-32-...)
  4. 将CardView用于按钮是错误的,请在material.io中检查MaterialButton文档

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/Login"
            android:textSize="40sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView_loginMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:text="@string/nice"
            android:textSize="32sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView" />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayoutUsername"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:padding="5dp"
            app:errorEnabled="true"
            app:errorTextAppearance="@style/ErrorStyleNormal"
            app:hintTextAppearance="@style/ErrorStyleNormal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textView_loginMessage">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Username"
                android:textSize="25sp" />

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

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayoutPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:padding="5dp"
            app:counterTextAppearance="@style/CounterStyleNormal"
            app:errorEnabled="true"
            app:errorTextAppearance="@style/ErrorStyleNormal"
            app:hintTextAppearance="@style/ErrorStyleNormal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textInputLayoutUsername"
            app:passwordToggleEnabled="true">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:inputType="textPassword"
                android:textSize="25sp" />
        </com.google.android.material.textfield.TextInputLayout>

        <TextView
            android:id="@+id/textViewForgotPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="16dp"
            android:text="@string/ForgotPassword"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textInputLayoutPassword" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/materialButton"
            style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            app:icon="@color/yourArrowIcon"
            app:iconGravity="start"
            android:text="yourText"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textViewForgotPassword" />

        <TextView
            android:id="@+id/textView_join"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="16dp"
            android:text="@string/To_Join_With"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/materialButton" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="16dp"
            android:text="@string/Click_here"
            android:textColor="@android:color/holo_blue_bright"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textView_join" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

这篇关于我是否需要更改xml代码以支持所有屏幕尺寸的android设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆