我已经准备好许多教程,我已经第三次阅读了http://developer.android.com/guide/practices/screens_support.html,但是我仍然不知道如何设置布局以兼容多屏。
在android docs上,我发现了这一点:
例如,具有layout_width =“ 100dp”的视图在中等密度屏幕上的宽度为100像素,而系统在高密度屏幕上将其缩放为150像素,因此该视图在屏幕上大约占据相同的物理空间。
好的,让我们看一个例子:
如您所见,分辨率是相同的(480x800),但是视图直到结束才填满。我知道我应该使用fill_parent或match_parent,但这仅用于测试目的。
XML布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:weightSum="100"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="320dp"
android:layout_height="0dp"
android:layout_weight="45"
android:background="@drawable/bg_red" >
</RelativeLayout>
<RelativeLayout
android:layout_weight="10"
android:layout_width="fill_parent"
android:layout_height="0dp" >
</RelativeLayout>
<RelativeLayout
android:layout_width="320dp"
android:layout_height="0dp"
android:layout_weight="45"
android:background="@drawable/bg_blue" >
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Dp或dip(density-independent pixels
)考虑了设备的密度。与密度无关的像素的目的是在任何密度的屏幕上以相同的物理尺寸显示视图。
倾斜等于多少实际像素取决于设备的密度:
在实际设备上,一切都变得更加清晰:
您的480 * 800 hdpi设备的物理尺寸将比480 * 800 mdpi设备的物理尺寸小。因此,当视图填充hdpi设备的屏幕时,具有相同物理尺寸(dp)的视图将不会填充mdpi设备的屏幕。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句