【问题标题】:Android Studio - Layout Elements differ in size on the physical device, compared to the preview and emulatorAndroid Studio - 与预览版和模拟器相比,物理设备上的布局元素大小不同
【发布时间】: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>

Preview in Android Studio

Emulator

Physical Device

【问题讨论】:

    标签: java android android-studio resolution screen-size


    【解决方案1】:

    我认为您在约束布局方面犯了很多新手错误。

    你说你是初学者,我建议你尝试使用相对布局。

    如果你想坚持约束布局,这里有一个快速教程,可以让你的约束布局基础更强大: Youtube:Code in Flow playlist for Constraint Layout

    快速提示:

    1. 如果您专注于获得正确的约束,请尽量保持 按钮的布局(高度和宽度)相同。

    对于其中一个按钮,您使用了代码块:

    android:layout_width="0dp" android:layout_height="wrap_content"

    对于您使用过的其他按钮:

    android:layout_width="156dp" android:layout_height="wrap_content"

    1. 非常重要; 考虑用力链接项目。不用担心!这是非常小的事情。浏览该链接后,一切都会好起来的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2012-04-20
      • 1970-01-01
      • 2023-04-06
      • 2013-03-13
      • 2021-07-30
      相关资源
      最近更新 更多