为什么我的活动在应用程序运行时显示方式与我的.xml文件中不同?

huangapple go评论59阅读模式
英文:

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>

问题在于,当我运行应用程序时,它显示的方式不同。请查看以下照片以查看差异:

为什么我的活动在应用程序运行时显示方式与我的.xml文件中不同?

左边是我想要的方式,但右边是应用程序运行时的方式,我不知道为什么会这样。有任何想法吗?
非常感谢。

英文:

Here´s my problem. I have a login activity in android. Here it is:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;@color/primary_color&quot;
tools:context=&quot;.activities.LoginActivity&quot;&gt;
&lt;!--&lt;EditText
android:id=&quot;@+id/editTextTextEmail&quot;
android:hint=&quot;Email&quot;
android:textColorHint=&quot;@color/primary_text&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;0dp&quot;
android:layout_marginStart=&quot;37dp&quot;
android:layout_marginEnd=&quot;37dp&quot;
android:layout_marginBottom=&quot;3dp&quot;
android:ems=&quot;10&quot;
android:inputType=&quot;textEmailAddress&quot;
android:text=&quot;&quot;
android:textColor=&quot;@color/primary_text&quot;
android:textSize=&quot;10sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/editTextUsername&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.0&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/editTextId&quot;
tools:ignore=&quot;TouchTargetSizeCheck&quot; /&gt;--&gt;
&lt;EditText
android:id=&quot;@+id/editTextPassword&quot;
android:layout_width=&quot;202dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginBottom=&quot;44dp&quot;
android:ems=&quot;10&quot;
android:hint=&quot;Contrase&#241;a&quot;
android:inputType=&quot;textPassword&quot;
android:text=&quot;&quot;
android:textAlignment=&quot;center&quot;
android:textColor=&quot;@color/primary_text&quot;
android:textColorHint=&quot;@color/primary_text&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/buttonLogin&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.497&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/editTextId&quot;
android:layout_width=&quot;191dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;40dp&quot;
android:ems=&quot;10&quot;
android:hint=&quot;ID&quot;
android:inputType=&quot;number&quot;
android:text=&quot;&quot;
android:textAlignment=&quot;center&quot;
android:textColor=&quot;@color/primary_text&quot;
android:textColorHint=&quot;@color/primary_text&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/editTextUsername&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.022&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/editTextUsername&quot;
android:layout_width=&quot;221dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginBottom=&quot;32dp&quot;
android:ems=&quot;10&quot;
android:hint=&quot;Nombre de usuario&quot;
android:inputType=&quot;text&quot;
android:textAlignment=&quot;center&quot;
android:textColor=&quot;@color/primary_text&quot;
android:textColorHint=&quot;@color/primary_text&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/editTextPassword&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
&lt;Button
android:id=&quot;@+id/buttonLogin&quot;
android:layout_width=&quot;152dp&quot;
android:layout_height=&quot;48dp&quot;
android:layout_marginBottom=&quot;16dp&quot;
android:background=&quot;#23F62B&quot;
android:text=&quot;Entrar&quot;
android:textColor=&quot;@color/primary_text&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.497&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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:
为什么我的活动在应用程序运行时显示方式与我的.xml文件中不同?

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:

 &lt;EditText
android:id=&quot;@+id/editTextPassword&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginBottom=&quot;44dp&quot;
android:ems=&quot;10&quot;
android:hint=&quot;Contrase&#241;a&quot;
android:inputType=&quot;textPassword&quot;
android:text=&quot;&quot;
android:textAlignment=&quot;center&quot;
android:textColor=&quot;@color/primary_text&quot;
android:textColorHint=&quot;@color/primary_text&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/buttonLogin&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.497&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;

答案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.

huangapple
  • 本文由 发表于 2023年6月12日 17:52:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定