0

我正在编写一个在 720p 屏幕上运行良好的应用程序。我想让这个应用程序在不同的屏幕尺寸上运行。我读了这篇文章:http: //developer.android.com/guide/practices/screens_support.html

但我很困惑。我为不同的布局(layout-normal、layout-large、layout-xlarge)创建了 3 个文件夹,并在每个文件夹中放置了一个不同的 XML 文件。但是有些屏幕布局正常,我的应用在其中一些上看起来不错,而在另一些上看起来很糟糕。例如,这些尺寸是正常布局:(4.7" WXGA, 4" WVGA, 3.7" WVGA, ...) 但我的应用在 4" 和 3.7" 上看起来不错,而在其他类型上看起来很糟糕。

我在 layout-normal 文件夹中的 activity-main.xml 是:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backasli"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="100dp"
        android:paddingTop="140dp"
        android:stretchColumns="3" >

        <TableRow
            android:id="@+id/tableRow6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <TextView
                android:id="@+id/txt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="pray :"
                android:textColor="#031a7b"
                android:textSize="15sp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <TextView
                android:id="@+id/namaztxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="#031a7b"
                android:textSize="20sp" />
        </TableRow>

        <TableRow android:gravity="center" >

            <Button
                android:id="@+id/buttonchange"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:background="@drawable/ubtn"
                android:text="change"
                android:textColor="#ffffff"
                android:textSize="20sp" />
        </TableRow>
    </TableLayout>

</LinearLayout>
4

1 回答 1

2

Android 提供了一种非常好的方式来支持多种屏幕尺寸。它被称为权重。您应该使用LinearLayout 权重并检查您的图形视图以确定您想要如何调整大小。

完全避免:

  1. 计算您的分辨率和屏幕尺寸。你最终会陷入混乱。
  2. 跨 dpi 的多个布局。

相信我,到目前为止我开发的所有应用程序 (12) 都是在所有平台上使用具有 1 个布局文件的权重完成的。它有效,并且像奇迹一样工作。

编辑:

我也从不使用超过 5dp 的填充和 10dp 的边距。这样我就可以保持代码干净,Android 权重会照顾我的可用空间。

有关权重的更多信息,请查看问题。一开始就很好解释。

再次编辑:

想想看。您假设 dp 在所有手机的物理毫米(毫米)方面都是“相同的”。但事实并非如此。事实上,X 和 Y 1 dp 可能在“mm”中表示不同的含义。dp 是“不”一致的。使用像 100dp 这样的代码并设置硬编码的宽度和高度时要小心。更糟糕的是,多个布局文件。你会陷入很多代码维护的噩梦。

于 2014-03-05T07:35:25.603 回答