android - Android:为什么材质组件不适合不同的屏幕?
问题描述
我有一个布局,我希望它的所有组件都适合所有可能的屏幕分辨率,但事实并非如此。不过,在 Pixel 2 上看起来与预期的一样。
layout.xml
, 中定义的每个常数dp
:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_background"
android:gravity="center"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/first_name_layout"
style="@style/MaterialTextInputLayout"
android:layout_width="@dimen/text_field_width"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_default"
android:hint="@string/sign_up_first_name">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:inputType="textPersonName" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/last_name_layout"
style="@style/MaterialTextInputLayout"
android:layout_width="@dimen/text_field_width"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_default"
android:hint="@string/sign_up_last_name">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/last_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:inputType="textPersonName" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/preferred_name_layout"
style="@style/MaterialTextInputLayout"
android:layout_width="@dimen/text_field_width"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_default"
android:hint="@string/sign_up_pref_name">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/preferred_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:inputType="textPersonName" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/phone_number_layout"
style="@style/MaterialTextInputLayout"
android:layout_width="@dimen/text_field_width"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_default"
android:hint="@string/sign_up_phone_number">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/phone_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:inputType="phone" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/email_layout"
style="@style/MaterialTextInputLayout"
android:layout_width="@dimen/text_field_width"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_default"
android:hint="@string/sign_up_email">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password_layout"
style="@style/MaterialTextInputLayout"
android:layout_width="@dimen/text_field_width"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_default"
android:hint="@string/sign_up_password"
app:endIconMode="password_toggle">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password_confirmation_layout"
style="@style/MaterialTextInputLayout"
android:layout_width="@dimen/text_field_width"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_default"
android:hint="@string/sign_up_password_confirmation"
app:endIconMode="password_toggle">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/password_confirmation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/submit_btn"
android:layout_width="@dimen/default_button_width"
android:layout_height="@dimen/default_button_height"
android:layout_marginTop="@dimen/margin_big"
android:elevation="2dp"
android:text="@string/wizard_sign_up_submit"
android:textColor="@color/white" />
</LinearLayout>
</layout>
从左到右:
解决方案
一个 android SDK,它提供了一个新的尺寸单位 - sdp (scalable dp)。此尺寸单位随屏幕尺寸缩放。它可以帮助Android开发者支持多屏。
我想建议你使用sdp (scalable dp)
例子
android:layout_marginTop="@dimen/_10sdp"
android:layout_width="@dimen/_100sdp"
将 sdp 添加到您的项目中
dependencies {
implementation 'com.intuit.sdp:sdp-android:1.0.6'
}
供参考
阅读使用 ConstraintLayout 构建响应式 UI。
为不同的屏幕尺寸创建响应式布局的最佳方法是使用 ConstraintLayout 作为 UI 中的基本布局。ConstraintLayout 允许您根据与布局中其他视图的空间关系为每个视图指定位置和大小。这样,所有视图都可以随着屏幕尺寸的变化一起移动和拉伸。
推荐阅读
- sqlite - 在一个表中最多保留 5 行(Room/Sqlite)
- c - 在使用系统调用制作的 printf 上输出两个字符串时遇到问题
- c - 需要一点帮助来修复 Arduino RFID 程序
- mysql - 用 '\/' 替换反斜杠 '\\' 的 sql 查询
- javascript - 第 1 行出现意外的令牌“{”
- java - getIntent() 返回 null
- python - 由 maplotlib 图组成的 kivy 滚动视图不滚动
- python - 在 AWS EC2 实例的端口 80 启动烧瓶应用程序时权限被拒绝
- java - 从 Java Web 应用程序调用 Oracle 存储过程
- python-3.x - 如何在 python 中使用 selenium 下载文件?