首页 > 解决方案 > 我的手机测试中的布局错误配置问题

问题描述

在我的手机上进行测试时,我的布局完全变形,整个界面都在屏幕顶部,使测试变得困难。

我的电脑配置太低,我无法模拟,但我的手机分辨率非常高,我不知道这是否是布局错误的原因。

在此处输入图像描述

    <?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:orientation="vertical"
        tools:context=".RegisterPerson">
    
        <EditText
            android:id="@+id/editName_people"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter the person's name"
            android:inputType="textPersonName"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="94dp" />
    
        <EditText
            android:id="@+id/editEmail_person"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter the person's email"
            android:inputType="textEmailAddress"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="155dp" />
    
        <EditText
            android:id="@+id/editTelephone_person"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="enter the person's telephone"
            android:inputType="phone"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="213dp" />
    
        <Button
            android:id="@+id/button_insert_person"
            android:layout_width="161dp"
            android:layout_height="49dp"
            android:background="#397939"
            android:text="Register person"
            android:textAlignment="center"
            android:textColor="@android:color/background_light"
            android:textStyle="bold"
            tools:layout_editor_absoluteX="49dp"
            tools:layout_editor_absoluteY="298dp" />
    
        <Button
            android:id="@+id/button_list_persons"
            android:layout_width="143dp"
            android:layout_height="49dp"
            android:background="#171688"
            android:text="List person"
            android:textAlignment="center"
            android:textColor="@android:color/background_light"
            android:textStyle="bold"
            tools:layout_editor_absoluteX="238dp"
            tools:layout_editor_absoluteY="298dp" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="210dp"
            android:layout_height="wrap_content"
            android:text="Person data"
            android:textAlignment="center"
            android:textSize="24sp"
            tools:layout_editor_absoluteX="100dp"
            tools:layout_editor_absoluteY="40dp" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

标签: javaandroid-studio

解决方案


如果您使用ConstraintLayout,我建议您使用约束。例如,您可以将第一个EditText放置在顶部,ConstraintLayout另一个EditText放置在其下方。

例如:

 <EditText
        android:id="@+id/editName_people"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter the person's name"
        android:inputType="textPersonName"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editEmail_person"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter the person's email"
        android:inputType="textEmailAddress"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toBottomOf="@id/editName_people" />

app:layout_constraintTop_toTopOf="parent"

将元素的顶部定位在 wrapping 的顶部ConstraintLayout

app:layout_constraintTop_toBottomOf="@id/editName_people"

将元素的顶部定位到由@id 寻址的元素的底部。

通过使用 android:layout_marginTop&android:layout_marginBottom您可以添加元素之间的垂直距离。

通过使用其中一些约束,您可以水平放置元素。

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"

app:layout_constraintLeft_toRightOf="@id/..."
app:layout_constraintRight_toLeftOf="@id/..."

此外,使用 dp 值作为宽度/高度,您可以保存dimens.xml并通过其 id 访问它。

android:layout_width="@dimen/tv_width"
android:layout_height="@dimen/tv_height"

推荐阅读