[英]Supporting Different Screen Sizes With modern techniques as of 2019
這個問題在 SO 上被問了超過 100 次,例如這個問題是在 8 年前被問到的。 並且可以肯定的是,討論中定義的方法已經過時了。
隨着新的設計組件(如約束布局)和桶的變化(如 MipMap)以及其他技術的變化,我現在想討論現代技術。
在專門設計時,使應用程序支持大量設備的最佳實踐是什么? 我在 android 方面有超過 3 年的經驗,但我仍然熱衷於學習通常在任何地方都沒有正式討論過的新事物。 那么你能告訴我必須遵循什么規則來支持不同的屏幕尺寸嗎?
例如,我將向您展示我們必須在所有設備上看起來都一樣的屏幕
<?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>
這在移動設備上看起來非常好,但在平板電腦和大屏幕上看起來最差。
請在手機屏幕上查看下圖。
在平板電腦上查看下圖
問題:應該怎么做才能在平板電腦中顯示居中漂亮的布局,而不是全屏編輯文本。 換句話說,應該遵循什么來根據新技術在所有設備上顯示相同的布局?
注意:我知道如果我們應用一些屬性,我們可以在給定的示例中限制 edittext 的寬度,但我們不能在其他復雜布局中使用這些屬性。
也許您可以嘗試以下自動管理所有屏幕尺寸分辨率的庫。 我在我當前的項目中使用它。
對於維度
implementation 'com.intuit.sdp:sdp-android:1.0.6'
對於文本大小
implementation 'com.intuit.ssp:ssp-android:1.0.6'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.