java - 我的手机测试中的布局错误配置问题
问题描述
在我的手机上进行测试时,我的布局完全变形,整个界面都在屏幕顶部,使测试变得困难。
我的电脑配置太低,我无法模拟,但我的手机分辨率非常高,我不知道这是否是布局错误的原因。
- 在我的手机上,界面看起来像这样,但事实并非如此。
- 代码布局
<?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>
解决方案
如果您使用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"
推荐阅读
- mysql - 如何区分标准 SQL 中的两个 DATETIME 列?
- vue.js - Vuetify $refs.form.reset 和 $refs.form.resetValidation 给出错误
- paypal - Paypal 支付 API 错误 DoDirectPayment 失败:(28)
- python - Python - 类似人类的鼠标行为
- powershell - 如何强制 Powershell 查看命令的输出并等待它完成
- html - Css wont link to HTML even though it worked before
- bash - Shell脚本在后台并行运行函数并获取返回字符串
- c# - RoutingProvider.GetRoute(...) 返回 null,出了什么问题,是否有不需要 API 密钥的 GMapProvider?
- excel - Getting the sum of second column where column A contains specific characters but exclude a certain letter
- linux - Is the memory returned from mmapping /dev/shm Write-Back (WB) or Non-Cacheable Write-Combining (WC) on Linux/x86?