【发布时间】:2021-04-19 15:27:57
【问题描述】:
我对 android 比较陌生。在设计我的布局时,我偶然发现了一个我无法解决的问题,而且似乎在任何地方都没有得到解答。或者也许我真的不知道我要搜索什么。无论如何,问题在于与实际设备相比,在模拟器中预览和显示布局的方式。如下图所示,布局元素和间距已关闭。我希望一切都更大,更紧密,更远。预览版和模拟器使用适用于物理测试设备(一加 8 Pro)的规格。
在其他问题中,我看到答案说,我使用 dp 对某些尺寸进行硬编码可能是一个问题,因为显示器具有高 dpi(确切地说是 xhdpi),但即使仅使用约束或使用 px 预览模拟器总是与实际设备上的应用程序不同。 以下是图片和xml:
<?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=".EntryActivity">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity=""
android:layout_marginTop="416px"
android:gravity="center_horizontal"
android:orientation="vertical"
android:text="@string/header_text"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/buttonPlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="336dp"
android:adjustViewBounds="true"
android:backgroundTint="#00FFFFFF"
android:onClick="changeActivity"
android:scaleType="fitCenter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:srcCompat="@drawable/button_play_02" />
<ImageButton
android:id="@+id/buttonCreate"
android:layout_width="156dp"
android:layout_height="wrap_content"
android:layout_marginTop="340dp"
android:layout_marginEnd="20dp"
android:adjustViewBounds="true"
android:backgroundTint="#00FFFFFF"
android:onClick="changeActivity"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.993"
app:layout_constraintStart_toEndOf="@+id/buttonPlay"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:srcCompat="@drawable/button_create" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
标签: java android android-studio resolution screen-size