【问题标题】:Create a multiple screen android application创建一个多屏安卓应用
【发布时间】:2012-07-24 15:15:04
【问题描述】:

我正在开发一个 Android 2.3.3 应用程序,该应用程序可在所有 Android 手机或平板设备上运行,并且仅支持 portrait

阅读Supporting Multiple Screens 我看到一张有很多屏幕尺寸和密度的表格。

我需要多少种布局?每个屏幕尺寸和密度一个?

我想我只需要四个:一个用于小型、普通、大型和 x-large。

【问题讨论】:

  • 实际上,这取决于您的要求。如果您还想支持横向,那么您必须为 layout-land、layout-large-land 等创建布局。
  • 它只支持纵向。
  • 通常只提供一种布局就足够了。如果您要支持非常小或非常大的设备,您可能想尝试一下并为这些设备提供布局。
  • 我还建议您只使用一种具有独立标准的布局和设计。为了支持多个屏幕,请使用不同尺寸的可绘制对象..
  • +1 我会选择@user370305 => 创建一个布局并为不同的屏幕和密度提供图像。

标签: android android-layout


【解决方案1】:

你基本上需要四个。但是,如果您希望您的应用程序根据不同的屏幕尺寸 AND 不同的方向表现不同,您应该使用:

/layout-port         > for medium layout portrait
/layout-land         > for medium layout landscape
/layout-xlarge-port  > for xlarge layout portrait
/layout-xlarge-land  > for xlarge layout landscape

等等

【讨论】:

  • @CodersParadise。我需要四个,还需要 ldpi、mdpi、hdpi 和 xhdpi 图像,不是吗?
  • 正如@PareshMayani 所考虑的那样,您不需要所有不同的布局或不同的图像密度。这些只会使您的应用程序在不同的屏幕尺寸下看起来很漂亮。如果您放置不同密度的图像,android 将根据设备选择其中之一。如果您只有一个密度图像(例如 mdpi),android 将始终使用该图像(也适用于不同密度的屏幕)。
【解决方案2】:

从您提供的链接:

By default, Android resizes your application layout to fit the current device screen. 
In most cases, this works fine.

因此,一般规则是在布局 xml 中使用与密度无关的像素 (dip) 来定义尺寸,其余部分由操作系统处理。这样做您就拥有适用于所有设备范围的唯一布局。

制作单独的布局并不是很常见的做法。只有当您有无法拉伸的图像时,这才是真正推荐的方式。在这种情况下,根据屏幕的大小和密度拆分图形无论如何都可以解决您的问题。

【讨论】:

  • 感谢您的回答。我喜欢这部分:“在大多数情况下,这很好用。”相同的布局在 HTC Desire 和三星 Galaxy Tab 7.7 上看起来不同。
  • 如果您关注Best Practices,相同的布局在包括平板电脑在内的不同设备上通常看起来相同。这就是我回答的重点。您可能会根据屏幕密度为您的应用程序考虑不同大小的可绘制对象。
  • 是的,我有 ldpi、mdpi、hdpi 和 x-hdpi 的不同图像。
【解决方案3】:

一个布局足以满足大密度设备,如果您还想实现x-large屏幕尺寸的布局,那么您需要制作另一个布局, 只需按照 ldpi 、 mpdi 和 hdpi 获取图像,将它们放置在相关文件夹中, 使用适当的布局权重创建布局,为背景提供完整的大小,如填充父级和按钮的确切大小

喜欢

<?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/bg"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bar"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight=".55"
                android:gravity="left|center"
                android:orientation="vertical" >

                <Button
                    android:id="@+id/goBack"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:background="@drawable/back_btn" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight=".25"
                android:gravity="center"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Forget Paaword"
                android:textColor="#ffffff"
                    android:textSize="18dp"
                    android:textStyle="italic" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight=".55"
                android:gravity="right|center"
                android:orientation="vertical" >

                <Button
                    android:id="@+id/hombutton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:background="@drawable/home1" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".2"
        android:gravity="right|center"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight=".5"
            android:gravity="right|center"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:text="User Name"
                android:textColor="#ffffff" />

            <EditText
                android:id="@+id/usernametext"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight=".5" android:imeOptions="actionDone">

                <requestFocus />
            </EditText>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight=".5"
            android:gravity="right|center"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:text="Email Id"
                android:textColor="#ffffff" />

            <EditText
                android:id="@+id/emailtextfp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:inputType="textEmailSubject" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight=".5"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/loginbuttonfp"
                android:layout_width="75dp"
                android:layout_height="28dp"
                android:layout_margin="5dp"
                android:background="@drawable/btn" android:text="Ok" android:textColor="#ffffff"/>

            <Button
                android:id="@+id/cancelbutton"
                android:layout_width="75dp"
                android:layout_height="28dp"
                android:layout_margin="5dp"
                android:background="@drawable/btn" android:text="Cancel" android:textColor="#ffffff"/>
        </LinearLayout>
    </LinearLayout>

</LinearLayout> 

【讨论】:

  • 感谢您的回答。三星 Galaxy Tab 7.7 的屏幕尺寸超大?
  • 虽然我还没有使用过标签,但我认为它应该在 x-large 下,因为它的显示屏相当大 7.7” [2] 1280×800 (WXGA) Super AMOLED Plus
  • 好的,我将添加一个 x-large 布局。谢谢。
【解决方案4】:

我创建了一个工具,可让您缩放/调整平板电脑和小屏幕设备的布局,并在此处发表了一篇博文:http://onemanmobile.blogspot.com/2012/04/how-to-scale-your-android-layouts-to.html

基本上,如果您希望您的应用适合所有设备和平板电脑,那么以 dp 单位为一种尺寸定义布局是不够的,因为有四种不同的“密度桶”。

此工具将允许您将布局转换为从默认基线拟合这些密度桶。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2014-01-23
    • 2011-07-31
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多