Android普通布局在设备上的显示方式不同
我在应用程序开发领域相对较新,但仍然无法找到正确的方法来设置xml文件(在本例中为普通布局),以适合使用此布局类型的每个移动设备 例如:我有一个Pixel 2模拟器和一个Nexus 5模拟器(都使用普通布局)。但是,屏幕上的结果在设备上看起来不同: 像素2(1080x1920-420dpi): Nexus 5(1080x1920-xxhdpi): 在谷歌开发者部分关于设备兼容性的一些研究之后,我发现这可能是由于不同的像素密度造成的,但我不知道如何解决这个问题 此布局的xml代码:Android普通布局在设备上的显示方式不同,android,android-studio,android-layout,Android,Android Studio,Android Layout,我在应用程序开发领域相对较新,但仍然无法找到正确的方法来设置xml文件(在本例中为普通布局),以适合使用此布局类型的每个移动设备 例如:我有一个Pixel 2模拟器和一个Nexus 5模拟器(都使用普通布局)。但是,屏幕上的结果在设备上看起来不同: 像素2(1080x1920-420dpi): Nexus 5(1080x1920-xxhdpi): 在谷歌开发者部分关于设备兼容性的一些研究之后,我发现这可能是由于不同的像素密度造成的,但我不知道如何解决这个问题 此布局的xml代码: <?xm
<?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>
所有的图像稍后将被矢量图形所取代
我现在的问题是,如何使Nexus 5(以及分辨率为1080x1920或通常使用普通布局的任何其他移动设备)上的布局与像素2上的布局相同?我长期以来一直在努力解决此类问题 这种情况通常发生在你在边距和填充物中弄乱了太多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>
只是说你会依恋你父母的左右,把视线放在屏幕中间。实际上是任何屏幕的。不要添加边距,而是将文本视图放在layoutgood point@GabrieleMariotti的中心位置,但是如果我想在播放按钮下添加两个按钮,第二个按钮将在底部被Nexus 5的屏幕切断,因此仍然有一个安排问题请回答类似的问题。好的,我只是将所有内容都更改为约束布局,它按照我希望的方式工作。谢谢,问题的确是dp利润。太好了!如果你想知道动画,记得查一下运动布局。