在Android Studio和我的设备上分辨率相同,但外观不同。

huangapple go评论92阅读模式
英文:

Same resolution in Android Studio and my Device but different looks

问题

我目前正在编写一个 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

英文:

I am currently programming an Android app and using Kotlin and Android 7, but when I run the app on my phone the picture is a bit different than when I design it in UI Designer. I have chosen the same resolution for both and I am stumped because I have had little contact with the frontend so far. Hopefully someone can help me, thanks already!

XML:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:background=&quot;@android:color/background_light&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

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

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


&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Android Studio
||| My Device

答案1

得分: 3

&lt;androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    &gt;

    &lt;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"
        /&gt;

    &lt;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"
        /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Result:

在Android Studio和我的设备上分辨率相同,但外观不同。

It will always center the button. If You want the center image (play button) to be a little lower You can add bias app:layout_constraintVertical_bias="0.75". Now the center image will be at 1/4 hight from the bottom.

<hr>

But I think You should newer hardcode margins to place somewhere on the screen. E.g. You have added android:layout_marginTop="583.7dp" this will only work on one screen resolution and on every other it will look bad. If You want it to be on the bottom just set the bottom constraint to the bottom of the parent and add a 16dp margin.

英文:

Try something like this:

&lt;androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    &gt;

    &lt;ImageView
        android:id=&quot;@+id/imageBackground&quot;
        android:layout_width=&quot;194.3dp&quot;
        android:layout_height=&quot;144.3dp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:srcCompat=&quot;@drawable/ic_launcher_background&quot;
        /&gt;

    &lt;ImageView
        android:id=&quot;@+id/imageFront&quot;
        android:layout_width=&quot;47dp&quot;
        android:layout_height=&quot;53.3dp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;@id/imageBackground&quot;
        app:layout_constraintEnd_toEndOf=&quot;@id/imageBackground&quot;
        app:layout_constraintStart_toStartOf=&quot;@id/imageBackground&quot;
        app:layout_constraintTop_toTopOf=&quot;@id/imageBackground&quot;
        app:srcCompat=&quot;@drawable/ic_launcher_foreground&quot;
        /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Now You can paste this to another container. And the Result:

在Android Studio和我的设备上分辨率相同,但外观不同。

It will always center the button. If You want the center image (play button) to be a little lower You can add bias app:layout_constraintVertical_bias=&quot;0.75&quot;. Now the center image will be at 1/4 hight from the bottom.

<hr>

But I think You should newer hardcode margins to place somewhere on the screen. E.g. You have added android:layout_marginTop=&quot;583.7dp&quot; this will only work on one screen resolution and on every other it will look bad. If You want it to be on the bottom just set the bottom constraint to the bottom of the parent and add a 16dp margin.

huangapple
  • 本文由 发表于 2020年9月11日 17:28:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63844326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定