[英]Android doesn't resize with different screen layouts
我正在嘗試根據我使用的設備調整布局的大小。 我正在兩款不同的手機(三星 Galaxy S8+ 和索尼 Xperia Z2(甚至兩個不同的模擬器))上進行測試。
我在 Manifest 文件中添加了一些代碼。 我用正確的(據說)名稱(layout、layout-small、layout-large、layout-xlarge)創建了不同的文件夾。 我將我的代碼放入 values 文件夾,文件“dimens.xml”並從我的布局中調用它。 但似乎沒有任何效果。
我的屏幕發生變化的唯一時間是我將相應的文件修改到“布局”文件夾中。
我希望得到任何幫助或建議。 謝謝!
我已多次閱讀有關 Android Developer 和 Stack Overflow 的文檔,但似乎找不到解決方案。
AndroidManifest.xml 的片段
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.applicationpro">
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"
android:resizeable="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
res/layout-large/my_layout.xml 的片段
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg4"
android:orientation="vertical"
tools:context=".activity.LoginActivity">
<ProgressBar
android:id="@+id/loginPG"
android:theme="@style/FragmentsProgressBar"
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:layout_marginTop="100dp"/>
<android.support.design.widget.TextInputLayout
android:id="@+id/loginTIY"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_top_large">
<AutoCompleteTextView
android:id="@+id/loginTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_login"
android:maxLines="1"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
尺寸文件
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="standard_55">65dp</dimen>
<dimen name="standard_105">135dp</dimen>
<dimen name="standard_155">155dp</dimen>
<!-- Small Dimensions = "Medium Dimensions * 0.75" For Example: 210*.75 = 157.5-->
<dimen name = "margin_top_small">150dp</dimen>
<!-- Medium Dimensions -->
<dimen name = "margin_top_medium">200dp</dimen>
<!-- Large Dimensions = "Medium Dimensions * 1.5" For Example: 210*1.5 = 315 -->
<dimen name = "margin_top_large">300dp</dimen>
<!-- XLarge Dimensions = "Medium Dimensions * 2" For Example: 210*1.5 = 420 -->
<dimen name = "margin_top_xLarge">400dp</dimen>
</resources>
TL;DR這是另一種使用ConstraintLayout
方法,而不是針對每個屏幕尺寸的單一布局
您可以使用ConstraintLayout支持不同的屏幕尺寸:
ConstraintLayout 允許您創建具有平面視圖層次結構(無嵌套視圖組)的大型復雜布局。 它與RelativeLayout 類似,所有視圖都是根據兄弟視圖和父布局之間的關系進行布局的,但它比RelativeLayout 更靈活,並且更易於與Android Studio 的布局編輯器一起使用。
您可以使用這些屬性來指定您的視圖大小(以百分比為單位):
app:layout_constraintWidth_percent=".5"
app:layout_constraintHeight_percent=".5"
例如,單個按鈕的高度和寬度都等於屏幕的 50%:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<Button
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent=".5"
app:layout_constraintHeight_percent=".5"/>
</androidx.constraintlayout.widget.ConstraintLayout>
它看起來像這樣:
我可以推薦使用ConstraintLayout與指南和Chains來支持不同的屏幕尺寸(除了我上面提到的 - app:layout_constraintWidth_percent
和app:layout_constraintHeight_percent
)。
乍一看,這可能看起來像很多工作,有些人可能想知道這是否真的值得付出努力,但這就是為什么我相信 ConstraintLayout 是構建 UI 的正確方法:
它真的很人性化。
ConstraintLayout 非常容易學習。
一旦你學會了它,你會發現你節省了大量的開發時間,因為制作你的 UI 非常快。
約束布局旨在支持不同的屏幕尺寸,因此無需為每個屏幕尺寸構建布局(這也與之前的優勢相結合——節省開發時間)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.