Actuellement, j'ai ce code pour cette page.
Mais mon aperçu devrait que mon bouton soit au centre de la mise en page de la seconde moitié, mais dans mon émulateur, il l'affiche toujours en haut de la mise en page.
/ p>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background_profile"> <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/profile_img" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/default_person_icon" app:civ_border_color="@android:color/black" app:civ_border_width="2dp" android:layout_centerInParent="true" android:layout_marginTop="100dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="User Email" android:textSize="28sp" android:textColor="@android:color/white" android:layout_below="@+id/profile_img" android:layout_centerHorizontal="true" android:layout_marginTop="10dp"/> </RelativeLayout> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <Button android:id="@+id/btn_change_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Change Password" app:cornerRadius="50dp" android:layout_marginStart="40dp" android:layout_marginEnd="40dp"/> <Button android:id="@+id/btn_sign_out" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Sign Out" app:cornerRadius="50dp" android:layout_marginStart="40dp" android:layout_marginEnd="40dp"/> </LinearLayout> </LinearLayout>
Voici l'image pour l'aperçu
https://i.stack.imgur.com/QLaot.jpg
et voici l'image de l'émulateur
3 Réponses :
Différents téléphones ont une taille d'écran différente , dans votre mise en page, vous utilisez une taille fixe sur votre vue (la taille fixe est android: layout_width = "100dp"
par exemple) et le résultat est que ce qui peut sembler bon sur un écran (l'écran de prévisualisation de votre studio Android) ne le sera pas sur un autre écran (votre téléphone réel).
Si vous souhaitez créer une disposition pour prendre en charge toutes les tailles d'écran, vous pouvez utiliser ConstraintLayout avec guidelines et Chaînes pour prendre en charge différentes tailles d'écran.
Voici un exemple d'utilisation de ConstaintLayout:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".MainActivity"> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent=".5" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toTopOf="@+id/guideline7" app:layout_constraintEnd_toStartOf="@+id/guideline9" app:layout_constraintStart_toStartOf="@+id/guideline8" app:layout_constraintTop_toTopOf="parent" tools:srcCompat="@tools:sample/avatars[1]" /> <TextView android:id="@+id/textView9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="Email" app:layout_constraintEnd_toEndOf="@+id/imageView2" app:layout_constraintStart_toStartOf="@+id/imageView2" app:layout_constraintTop_toBottomOf="@+id/imageView2" /> <Button android:id="@+id/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/guideline9" app:layout_constraintStart_toStartOf="@+id/guideline8" app:layout_constraintTop_toTopOf="@+id/guideline7" /> <Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:text="Button" app:layout_constraintEnd_toStartOf="@+id/guideline9" app:layout_constraintStart_toStartOf="@+id/guideline8" app:layout_constraintTop_toBottomOf="@+id/button" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent=".1" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent=".9" /> </androidx.constraintlayout.widget.ConstraintLayout>
Voici comment look (je joins une image de l'éditeur de mise en page pour que vous puissiez voir la contrainte s et directives):
Vous pouvez utiliser la balise android: layout_weight
pour ne pas avoir à vous soucier des différentes tailles d'écran. Ici, j'ai fait un exemple en utilisant android: layout_weight, android: layout_gravity
et android: gravity
pour créer une structure dont vous avez besoin. J'espère que cela vous aide. La capture d'écran de la structure est ici .
Vous pouvez obtenir plus de références sur android: layout_weight de ici .
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@android:color/holo_blue_dark" > <ImageView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/profile_picture" android:src="@drawable/user_profile_picture" android:layout_centerInParent="true"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/profile_picture" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="User Email" android:textColor="@android:color/white" android:textSize="28sp" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical" android:gravity="center"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center_horizontal"> <Button android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btn_change_password" android:layout_gravity="bottom" android:text="change password"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center_horizontal"> <Button android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btn_sign_out" android:layout_gravity="top" android:text="sign out"/> </LinearLayout> </LinearLayout> </LinearLayout>
p>
Vous semblez faire les choses correctement. Je suppose que vous venez de manquer d'ajouter le weightSum dans la balise parent.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:weightSum="2" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background_profile"> <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/profile_img" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/default_person_icon" app:civ_border_color="@android:color/black" app:civ_border_width="2dp" android:layout_centerInParent="true" android:layout_marginTop="100dp" a/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="User Email" android:textSize="28sp" android:textColor="@android:color/white" android:layout_below="@+id/profile_img" android:layout_centerHorizontal="true" android:layout_marginTop="10dp"/> </RelativeLayout> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <Button android:id="@+id/btn_change_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Change Password" app:cornerRadius="50dp" android:layout_marginStart="40dp" android:layout_marginEnd="40dp"/> <Button android:id="@+id/btn_sign_out" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Sign Out" app:cornerRadius="50dp" android:layout_marginStart="40dp" android:layout_marginEnd="40dp"/> </LinearLayout> </LinearLayout>
Cela a semblé le faire fonctionner pour moi. Bonne chance!