英文:
Why my activity is not shown the same way when the app is running than in my .xml file
问题
这是我的问题。我在Android中有一个登录活动。这是它:
<?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"
android:background="@color/primary_color"
tools:context=".activities.LoginActivity">
<EditText
android:id="@+id/editTextPassword"
android:layout_width="202dp"
android:layout_height="wrap_content"
android:layout_marginBottom="44dp"
android:ems="10"
android:hint="Contraseña"
android:inputType="textPassword"
android:text=""
android:textAlignment="center"
android:textColor="@color/primary_text"
android:textColorHint="@color/primary_text"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/buttonLogin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/editTextId"
android:layout_width="191dp"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="ID"
android:inputType="number"
android:text=""
android:textAlignment="center"
android:textColor="@color/primary_text"
android:textColorHint="@color/primary_text"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/editTextUsername"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.022" />
<EditText
android:id="@+id/editTextUsername"
android:layout_width="221dp"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:ems="10"
android:hint="Nombre de usuario"
android:inputType="text"
android:textAlignment="center"
android:textColor="@color/primary_text"
android:textColorHint="@color/primary_text"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/editTextPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="152dp"
android:layout_height="48dp"
android:layout_marginBottom="16dp"
android:background="#23F62B"
android:text="Entrar"
android:textColor="@color/primary_text"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
问题在于,当我运行应用程序时,它显示的方式不同。请查看以下照片以查看差异:
左边是我想要的方式,但右边是应用程序运行时的方式,我不知道为什么会这样。有任何想法吗?
非常感谢。
英文:
Here´s my problem. I have a login activity in android. Here it is:
<?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"
android:background="@color/primary_color"
tools:context=".activities.LoginActivity">
<!--<EditText
android:id="@+id/editTextTextEmail"
android:hint="Email"
android:textColorHint="@color/primary_text"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="37dp"
android:layout_marginEnd="37dp"
android:layout_marginBottom="3dp"
android:ems="10"
android:inputType="textEmailAddress"
android:text=""
android:textColor="@color/primary_text"
android:textSize="10sp"
app:layout_constraintBottom_toTopOf="@+id/editTextUsername"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextId"
tools:ignore="TouchTargetSizeCheck" />-->
<EditText
android:id="@+id/editTextPassword"
android:layout_width="202dp"
android:layout_height="wrap_content"
android:layout_marginBottom="44dp"
android:ems="10"
android:hint="Contraseña"
android:inputType="textPassword"
android:text=""
android:textAlignment="center"
android:textColor="@color/primary_text"
android:textColorHint="@color/primary_text"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/buttonLogin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/editTextId"
android:layout_width="191dp"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="ID"
android:inputType="number"
android:text=""
android:textAlignment="center"
android:textColor="@color/primary_text"
android:textColorHint="@color/primary_text"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/editTextUsername"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.022" />
<EditText
android:id="@+id/editTextUsername"
android:layout_width="221dp"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:ems="10"
android:hint="Nombre de usuario"
android:inputType="text"
android:textAlignment="center"
android:textColor="@color/primary_text"
android:textColorHint="@color/primary_text"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/editTextPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="152dp"
android:layout_height="48dp"
android:layout_marginBottom="16dp"
android:background="#23F62B"
android:text="Entrar"
android:textColor="@color/primary_text"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The problem is that when I run the app, it is not shown the same way. Look at the following photo to see the difference:
On the left it is like I want it to be, but on the right it is when the app is running and I don´t know why that happens.
Any idea?
Thank you very much.
答案1
得分: 1
不要提供硬编码的宽度,只需使用match_parent代替。
例如:
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="44dp"
android:ems="10"
android:hint="Contraseña"
android:inputType="textPassword"
android:text=""
android:textAlignment="center"
android:textColor="@color/primary_text"
android:textColorHint="@color/primary_text"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/buttonLogin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent" />
英文:
Don't provide harcoded width just use match_parent instead
For example:
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="44dp"
android:ems="10"
android:hint="Contraseña"
android:inputType="textPassword"
android:text=""
android:textAlignment="center"
android:textColor="@color/primary_text"
android:textColorHint="@color/primary_text"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/buttonLogin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent" />
答案2
得分: -1
IDE和模拟器之间的配置可能不同。您在IDE中使用自定义设备作为API 33的参考,也许您的设备配置不同,导致差异。
英文:
The configuration between your IDE and your emulator might be different. You use a Custom device as reference in your IDE on API 33, maybe your device does not have the same configuration leading to differencies.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论