【问题标题】:Why android project output is different in Android emulator and Hardware device为什么android项目输出在Android模拟器和硬件设备中不同
【发布时间】:2020-08-14 21:46:21
【问题描述】:

当我在 android 模拟器 (Nexus 5X) 中运行我的 android 项目时,它会显示我设计的正确布局。但是如果我在硬件设备(三星 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>

【问题讨论】:

  • 这可能是由于设备和模拟器上的像素密度造成的。
  • 那么如果我用android studio开发android应用程序,它不适合所有的android设备吗?我应该为不同的设备使用不同的格式吗?
  • 是和否。您可以配置您的 UI,也可以选择为不同的显示进行布局。使用 ConstraintLayout 而不是线性或相对。 ??????

标签: android xml android-studio android-layout android-emulator


【解决方案1】:

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

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

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" 工具: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>
    

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多