亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android 不會根據不同的屏幕布局調整大小

Android 不會根據不同的屏幕布局調整大小

江戶川亂折騰 2023-05-17 14:52:03
我正在嘗試根據我使用的設備調整布局大小。我正在測試兩種不同的手機(三星 Galaxy S8+ 和索尼 Xperia Z2(甚至是兩種不同的模擬器))。我在清單文件中添加了一些代碼。我創建了具有正確(假定)名稱的不同文件夾(layout、layout-small、layout-large、layout-xlarge)。我將我的代碼放入值文件夾,文件“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">        </android.support.design.widget.TextInputLayout>
查看完整描述

1 回答

?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

TL;DR?這是另一種方法?ConstraintLayout,不是針對每種屏幕尺寸使用單一布局


您可以使用ConstraintLayout支持不同的屏幕尺寸:

ConstraintLayout 允許您創建具有平面視圖層次結構(無嵌套視圖組)的大型復雜布局。它與 RelativeLayout 相似,因為所有視圖都是根據兄弟視圖和父布局之間的關系進行布局,但它比 RelativeLayout 更靈活,并且更容易與 Android Studio 的布局編輯器一起使用。

您可以使用這些屬性以百分比指定視圖大?。?/p>

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>

它看起來像這樣:

http://img4.sycdn.imooc.com/646479c30001310303540628.jpg

我可以推薦使用ConstraintLayout?with?guidelines和Chains來支持不同的屏幕尺寸(除了我上面提到的 -app:layout_constraintWidth_percentapp:layout_constraintHeight_percent)。


乍一看,這可能需要大量工作,有些人可能想知道這是否真的值得付出努力,但這就是為什么我相信 ConstraintLayout 是構建 UI 的正確方法:

  • 它真的很用戶友好。

  • ConstraintLayout 非常簡單易學。

  • 一旦你學會了它,你會發現你節省了大量的開發時間,因為制作你的 UI 非常簡單。

  • 約束布局旨在支持不同的屏幕尺寸,因此無需為每個屏幕尺寸構建布局(這也與之前的優勢相關——節省開發時間)。


查看完整回答
反對 回復 2023-05-17
  • 1 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號