为什么 android 项目输出在 Android 模拟器和硬件设备中不同

Why android project output is different in Android emulator and Hardware device

当我在 android 模拟器 (Nexus 5X) 中 运行 我的 android 项目时,它显示了我设计的正确布局。但是如果我 运行 硬件设备 (Samsung Galaxy J2 Prime)中的项目它显示不同的输出。(边距不完全正确在硬件设备中它粘在底部,查看底部的边距) 1 - 硬件设备的输出 2 - 模拟器的输出

这是 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/background"
    tools:context=".OrderStatus">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/listOrders"
            android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="550dp">


        </androidx.recyclerview.widget.RecyclerView>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/listCalls"
            android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="100dp">


        </androidx.recyclerview.widget.RecyclerView>


    </LinearLayout>


</RelativeLayout>

要完成这项工作,请执行以下操作:

  • 将recyclerView的大小类型从dp(密度像素)改为sp(可缩放像素)

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" tools:context=".OrderStatus">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/listOrders"
            android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="550sp">
    
    
        </androidx.recyclerview.widget.RecyclerView>
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/listCalls"
            android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="100sp">
    
    
        </androidx.recyclerview.widget.RecyclerView>
    
    
    </LinearLayout>
    

这种布局缩放比例的变化也因设备而异。

如果您想在不同的设备上协调布局,请不要使用 dp 大小。这取决于 phone 的大小。最好使用约束布局。以我对这个问题的回答为例,使用约束布局:How to prevent one activity from resizing when keyboard opens