支持多屏幕尺寸Android

时间:2014-10-29 17:44:47

标签: android layout android-screen-support

如何优化我的应用以支持多种屏幕尺寸? 我正在开发一个项目,其中大多数图形是XML drawables,没有定义的大小。 我已尝试使用维度值文件夹(ldpi,mpdi,etcetc),但这并没有真正完成工作。我被困在屏幕上的一些项目,所以我尝试为每个屏幕尺寸创建不同的布局,但同样的事情发生,应用程序看起来很好4.3 ... 5""设备,但当我启动3.7模拟器时,应用程序看起来很糟糕,有些项目不在屏幕/其他项目覆盖。 我使用相对布局,我没有看到代码的需要,但如果需要我会发布。 我的问题是:优化应用以支持多种屏幕尺寸的最佳方法是什么?

3 个答案:

答案 0 :(得分:1)

我认为您的出发点应该是通过Google查看此页面:https://developer.android.com/guide/practices/screens_support.html

但是,这里有一些指示:

  • 自由使用" wrap_content"和" match_parent"高度和宽度布局属性。避免,尽可能设置明确的尺寸
  • 当您需要设置显式大小时,请考虑将dimen.xml用于不同的屏幕或考虑以编程方式设置值
  • 确保在您的drawable中有不同的res图像,不要忘记drawables-nodpi文件夹

答案 1 :(得分:0)

我发现的最好方法是在用XML定义布局时要非常周到。在大多数情况下,您会发现可以定义布局,以便在设备之间进行适当缩放,直到达到真正的高端。

以此布局为例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLinView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/KeywordItemViewMain"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="@dimen/defViewMargin"
    android:layout_marginRight="@dimen/defViewMargin"
    android:layout_marginTop="@dimen/defViewMarginTopBottom"
    android:layout_marginBottom="@dimen/defViewMarginTopBottom"
    android:background="@drawable/rounded_corners_background"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="@dimen/defViewPadding" >

    <TextView
        android:id="@+id/rowKeywordTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="5dp"
        android:text="@string/keywordrow_name"
        android:textSize="15sp"
        android:textStyle="bold" />

</LinearLayout>

<LinearLayout
    android:id="@+id/icons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/KeywordItemViewMain"
    android:layout_centerVertical="true" >

    <ImageView
        android:id="@+id/acknowledgedIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/handled"
        android:padding="5dp"
        android:src="@drawable/acknowledge_item_icon" />
</LinearLayout>

填写家长&amp; Wrap Content将是您最好的朋友。

当我真的需要在手机之间指定不同设计的布局或者在必要时使用真正的高/低端屏幕尺寸时,我只使用小,普通,大和X大的文件夹,因为我觉得制作4 /很烦人每次我想对布局进行微小修改时都会进行5次编辑。

答案 2 :(得分:-1)

您应为每种屏幕尺寸(小尺寸,普通尺寸,大尺寸和X尺寸)创建4个文件夹。 右键单击编辑器中的res文件夹,然后选择New - &gt; AndroidResourceDirectory从Available qualifier中选择size并创建各种大小的文件夹。 祝你好运