我更新我的Android應用程序,並意識到,我已經創建了一個佈局爲每個可能的屏幕尺寸(佈局小,佈局大型,等...),這將是一個巨大的痛苦瀏覽每個XML文件並手動進行小改動。我正在嘗試創建一個XML文件來支持所有的屏幕尺寸。在查看了Android文檔和其他關於stackoverflow的問題之後,似乎LinearLayout是最好的選擇,因爲您可以爲佈局中的每個項目提供weightSum和layout_weight。這不像預期的那樣工作(見下面的代碼和圖像)。我正確地做到了這一點?我是否必須回到爲每種可能的屏幕尺寸創建RelativeLayout?的Android單個佈局,以適應所有屏幕尺寸
我的圖像是一個繪製的文件夾和我的佈局是在一個單一的佈局文件夾。
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:gravity="center"
android:orientation="vertical"
android:weightSum="100" >
<ImageButton
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/image0"
android:background="@null"
android:layout_weight="30" />
<ImageButton
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/image1"
android:background="@null"
android:layout_weight="30" />
<ImageButton
android:id="@+id/key"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="30"
android:background="@null"
android:src="@drawable/image0_key" />
<TextView
android:id="@+id/tvScore"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Score: 0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="10"
android:layout_gravity="left" />
</LinearLayout>
結果視圖
歌Nexus One(項目及佈局的屏幕尺寸不相符的溢出):
平板電腦:
編輯: 我已經添加了以下可繪製-____文件夾。它產生相同的結果。
每個孩子的layout_height應0dp和根據重量分配。你的textview也有一個match_parent,這將強制電視佔用和父母一樣多的空間(覆蓋所有屏幕的線性佈局) –
將每個孩子的layout_height更改爲0dp後,沒有任何變化。看起來和上面的照片完全一樣。 – Shane
你需要了解重量是如何工作的。佈局重量在佈局測量完成後(第二遍)分佈,因此您必須提供每個顯示密度具有相同密度的圖像,否則不需要做任何事情。系統在放置圖像後使用FREE空間。 –