首页 > 解决方案 > 截至 2019 年,使用现代技术支持不同的屏幕尺寸

问题描述

这个问题在 SO 上被问了 100 多次,例如这个问题是 8 年前问的。当然,讨论中定义的方法已经过时了。

随着新的设计组件(例如约束布局)和存储桶中的更改(例如 MipMap)以及其他技术更改,我现在想讨论现代技术。

在专门设计时,使应用程序支持大量设备的最佳实践是什么。我有超过 3 年的 android 经验,但我仍然热衷于学习通常不会在任何地方正式讨论的新事物。那么你能告诉我必须遵循哪些规则来支持不同的屏幕尺寸吗?

例如,我将向您展示我们必须在所有设备上看起来都一样的屏幕

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"    
    android:orientation="vertical"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_height="wrap_content"
    android:layout_margin="30dp"
    android:gravity="center"
    >

    <TextView
        android:id="@+id/lblLogo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/app_name"
        android:autoSizeTextType="uniform"
        android:gravity="center"
        android:padding="15dp"
        />


    <!-- Email Label -->
    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilLoginEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etLoginEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="Email" />

    </com.google.android.material.textfield.TextInputLayout>



    <!-- Password Label -->
    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilLoginPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etLoginPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="Password"/>

    </com.google.android.material.textfield.TextInputLayout>



    <com.google.android.material.button.MaterialButton
        android:id="@+id/btnLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:padding="12dp"
        android:text="@string/btnLogin"/>
</LinearLayout>

这在移动设备上看起来非常好,但在平板电脑和大屏幕上看起来最差。

在手机屏幕上看下图。

在此处输入图像描述

在平板电脑上查看下图

在此处输入图像描述

问题:应该怎么做才能在平板电脑中显示居中的漂亮布局,而不是全屏编辑文本。换句话说,应该遵循什么来根据新技术在所有设备上显示相同的布局?

注意:我知道如果我们应用一些属性,我们可以在给定示例中限制编辑文本的宽度,但我们不能在其他复杂布局中使用这些属性。

标签: androidlayoutandroid-constraintlayout

解决方案


也许您可以尝试以下自动管理所有屏幕尺寸分辨率的库。我在我目前的项目中使用它。

对于维度

implementation 'com.intuit.sdp:sdp-android:1.0.6'

对于文本大小

implementation 'com.intuit.ssp:ssp-android:1.0.6'

欲了解更多信息https://github.com/intuit/sdp


推荐阅读