0

我正在android中测试多dpi,布局xml文件看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/new_album_bg">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
    <RelativeLayout android:gravity="center"
        android:paddingTop="400dip" android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <EditText android:background="@drawable/name_text_bg"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:gravity="center" />
    </RelativeLayout>
</RelativeLayout>

当我在 480x800 mdpi(160) 模拟器(缩放到 7 英寸)上运行它时,它工作正常,但在 480x800 hdpi 模拟器上,它将 EditText 填充到屏幕底部附近,接近 600 像素,是 400dip x 240 / 160 ? 即使我将其缩放到 4 英寸,它似乎也与屏幕尺寸没有区别。如何让padding在不同的屏幕密度下工作?非常感谢!

4

1 回答 1

1
How to let padding work under different screen density? 

您可以在不同的文件夹中创建具有不同填充的不同布局 xml,例如

res/layout-mdpi 
res/layout-hdpi

然后android会选择合适的layout xml
About dp(Dip)
dp会保证视图在当前设备屏幕上被赋予合适的尺寸。例如,带有 a 的视图将在显示器和显示器上layout_width="100dp"测量100 pixels宽度,但视图将占据大致相同的物理空间。HVGA@160 density150 pixelsWVGA@240 density

有关更多详细信息,请参阅支持多个屏幕

于 2011-03-30T02:44:34.667 回答