Android Studio和我的设备分辨率一样,但显示效果不同

3

我目前正在编写一个Android应用程序,使用Kotlin和Android 7,但是当我在手机上运行该应用程序时,与我在UI设计师中设计的有些不同。我已经为两者选择了相同的分辨率,但是我很困惑,因为迄今为止我接触过很少的前端问题。希望有人可以帮助我,非常感谢!

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


嘿,Stackoverflow是用来寻求编程帮助的,如果你发布更多关于你的问题的细节,我们可以提供帮助。请发布你的XML代码、设计师界面和设备上的截图,然后我们可以看一下。 - Ruben Meiring
嘿@RubenMeiring,我现在已经编辑了我的问题。 - Juferdinand
有什么不同吗?我看不出任何区别。 - iknow
@iknow 图片大小会改变,播放按钮也会移动。 - Ruben Meiring
1
@Juferdinand 请检查iknows的答案,实际上他的解决方案比我的更简洁。 - Ruben Meiring
显示剩余3条评论
1个回答

3

可以尝试像这样:

<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边距即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接