Do I need to change my xml code for supporting all screen size android devices?

后端 未结 2 1916
抹茶落季
抹茶落季 2021-01-27 08:17

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 si

相关标签:
2条回答
  • 2021-01-27 08:50

    If you want to support different screen sizes, then use https://developer.android.com/reference/android/support/annotation/Dimension for different sizes of elements for different screens. If you need a different layout of elements on different screens, then you can create different xml files for different screen sizes Android layouts for different screen sizes

    0 讨论(0)
  • 2021-01-27 08:50

    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. Don't use match_parent with ConstraintLayout we only have 0dp (match_constraint) and wrap_conent
    2. Don't mess up the XML with unneccessary nested ViewGroups (LinearLayout)
    3. When using margin or padding best to use numbers based on 8 (8 - 16 - 24 - 32 - ...)
    4. Using a CardView for button is wrong, check MaterialButton documents at material.io
    <?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>
    
    0 讨论(0)
提交回复
热议问题