我更新我的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空间。 –