java - Android Studio 和我的设备中的分辨率相同但外观不同

标签 java android user-interface kotlin

我目前正在编写一个 Android 应用程序并使用 Kotlin 和 Android 7,但是当我在手机上运行该应用程序时,图片与我在 UI Designer 中设计时的图片有点不同。我为两者选择了相同的分辨率,但我很困惑,因为到目前为止我几乎没有接触过前端。希望有人能帮助我,已经谢谢了!

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"
    android:background="@android:color/background_light"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="194.3dp"
        android:layout_height="144.3dp"
        android:layout_marginStart="83dp"
        android:layout_marginTop="583.7dp"
        android:layout_marginEnd="82.7dp"
        android:layout_marginBottom="52dp"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/gui_base"
        tools:ignore="MissingConstraints"
        android:contentDescription="GUI" />

    <ImageView
        android:layout_width="47dp"
        android:layout_height="53.3dp"
        android:layout_marginStart="161.3dp"
        android:layout_marginTop="648dp"
        android:layout_marginEnd="151.7dp"
        android:layout_marginBottom="78.7dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/homebutton"
        tools:ignore="MissingConstraints"
        android:contentDescription="HomeButton" />


</androidx.constraintlayout.widget.ConstraintLayout>

Android Studio ||| My Device

最佳答案

尝试这样的事情:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:id="@+id/imageBackground"
        android:layout_width="194.3dp"
        android:layout_height="144.3dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_background"
        />

    <ImageView
        android:id="@+id/imageFront"
        android:layout_width="47dp"
        android:layout_height="53.3dp"
        app:layout_constraintBottom_toBottomOf="@id/imageBackground"
        app:layout_constraintEnd_toEndOf="@id/imageBackground"
        app:layout_constraintStart_toStartOf="@id/imageBackground"
        app:layout_constraintTop_toTopOf="@id/imageBackground"
        app:srcCompat="@drawable/ic_launcher_foreground"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

现在您可以将其粘贴到另一个容器中。而结果:

enter image description here

它总是让按钮居中。如果你想让中心图像(播放按钮)稍微低一点,你可以添加偏差 app:layout_constraintVertical_bias="0.75"。现在中心图像将位于距底部 1/4 高的位置。


但我认为您应该更新硬编码边距以放置在屏幕上的某个位置。例如。您添加了 android:layout_marginTop="583.7dp" 这将只适用于一种屏幕分辨率,而在其他屏幕分辨率下它看起来会很糟糕。如果您希望它位于底部,只需将底部约束设置为父级的底部并添加 16dp 的边距。

关于java - Android Studio 和我的设备中的分辨率相同但外观不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63844326/

相关文章:

android - UI 更新后暂停

java - 静态类是单例吗?

java - Spring批量配置

android - 使用带有 MapView 的自定义绘图的奇怪绘图 Artifact

android - 滚动器中 startScroll 背后的逻辑?

JAVA GUI 代码没有按预期运行

java - JAVA IF ELSE语句在我的代码中导致错误

java - 在 glassfish Web 服务中加载属性或 XML

java - OpenCV/JavaCV Android 人脸检测初始化

java - 如何使用java中的线程创建多个掉落的对象?