提问者:小点点

Android正常布局在设备上的显示方式不同


我在应用程序开发领域相对较新,仍然无法找到正确的方法来设置xml文件(在本例中为普通布局),以适合使用这种布局类型的每一个移动设备。

例如:我有一个Pixel 2仿真器和一个Nexus5仿真器(都使用普通布局)。 但是,屏幕上的结果在设备上看起来不同:

像素2(1080x1920-420dpi):https://i.stack.imgur.com/5zmz3.png

Nexus 5(1080x1920-xxHDPI):https://i.stack.imgur.com/ssltb.png

在Google developers部分关于设备兼容性的一些研究之后,我发现这可能是由于不同的像素密度造成的,但是我不知道如何修复这个问题。

此布局的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@drawable/mybackground"
    tools:context=".Start">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerName"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:background="#00FFFFFF"
        android:padding="5dp"
        android:scrollbars="none"
        android:layout_below="@id/name_add"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="160dp"
        android:layout_marginStart="200dp"
        />

    <EditText
        android:id="@+id/name_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="200dp"
        android:width="180dp"
        android:ems="10"
        android:hint="Name"
        android:importantForAutofill="no"
        android:inputType="text"
        android:singleLine="true"
        android:textColor="#424242"
        android:textColorHint="#424242"
        app:backgroundTint="#585858" />

    <ImageButton
        android:id="@+id/btn_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/name_add"
        android:layout_marginBottom="11dp"
        android:layout_toEndOf="@id/name_add"
        android:layout_marginStart="9dp"
        android:src="@drawable/ic_plus"
        android:background="@android:color/transparent"
        android:contentDescription="add"/>

    <Button
        android:id="@+id/goToSelection"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="520dp"
        android:background="@drawable/play_btn"
        android:fontFamily="sans-serif"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
        android:text="@string/btn_play"
        android:textAllCaps="false"
        android:textSize="20sp"
        />

    <Button
        android:id="@+id/languagegbr"
        android:layout_width="45dp"
        android:layout_height="25dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="15dp"
        android:layout_marginBottom="15dp"
        android:background="@drawable/gbr"
        />

    <Button
        android:id="@+id/languagedr"
        android:layout_width="45dp"
        android:layout_height="25dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="15dp"
        android:layout_marginBottom="55dp"
        android:background="@drawable/germanyr"
        android:visibility="gone"/>

</RelativeLayout>

所有的图像稍后都将被矢量图形所取代。

我现在的问题是,我如何才能使我在Nexus5上的布局(以及其他任何分辨率为1080x1920或通常使用普通布局的移动设备)看起来和在Pixel 2上的一样?


共1个答案

匿名用户

这类问题我已经纠结很久了。

这种情况发生时,你搞乱了太多的DP在边距和衬垫通常。 考虑到DP在不同的手机尺寸之间保持几乎相等,你会得到不同的显示屏,比如你给我们展示的这款。

我的解决方法是使用约束布局。 它使用起来非常简单,而且它有很多优点。 如果没有特殊的原因不使用约束布局,我强烈建议您现在就开始。

在约束布局中,要解决错误,您需要做的就是将水平约束设置到父级,仅此而已。

<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=".login.YourActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>   

</androidx.constraintlayout.widget.ConstraintLayout>

只需说你将依附于父母的左和右,就可以将视图设置在屏幕的正中间。 任何屏幕上。