适用于所有设备的Android XML布局(小型/普通/大型/XLarge等) [英] Android XML Layout for all Devices (Small/Normal/Large/XLarge, etc)

查看:73
本文介绍了适用于所有设备的Android XML布局(小型/普通/大型/XLarge等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个支持所有屏幕尺寸的XML布局.在XML中,第一个元素是ImageView,第二个元素是TextView,第三个元素是Button,带有一个Image.因此,TextView应该是所有设备(小型,中型,大型,xLarge等)中的确切位置.

I want to create a XML Layout that will support all the Screen sizes. In the XML, first element is ImageView, second one is TextView and third one is Button with an Image. So the TextView should be the exact position in all the devices (small, medium, large, xLarge, etc).

我该怎么做?

这是XML输出,如下所示:

Here is the XML output should be look like:

这是我为标准/中等"布局创建的XML文件:

Here is the XML file that i created for the Normal/Medium Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/firstscreenimage" />

<RelativeLayout
    android:id="@+id/relativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true" >

    <EditText
        android:id="@+id/campa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/textView3"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="My Current\n Campaign" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/relativeLayout2"
    android:layout_alignParentLeft="true" >

    <Button
        android:id="@+id/button1"
        android:layout_width="210dp"
        android:layout_height="210dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/animation0" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="Start" />
</RelativeLayout>

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/relativeLayout1"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="42dp"
    android:gravity="right"
    android:paddingRight="25dp"
    android:text="0.0km"
    android:textSize="30dp" />

</RelativeLayout>

推荐答案

您需要将所有WidthsHeightsPaddingsMargins等放入/res/values/dimens.xml文件中,如下所示:

You need to put all the Widths, Heights, Paddings, Margins , etc in the /res/values/dimens.xml file like this:

dimens.xml :

<!-- Small Dimensions = "Medium Dimensions * 0.75" For Example: 210*.75 = 157.5-->
<dimen name = "button1_width_small">157.5dip</dimen>

<!-- Medium Dimensions -->
<dimen name = "button1_width_medium">210dip</dimen>

<!-- Large Dimensions = "Medium Dimensions * 1.5" For Example: 210*1.5 = 315 -->
<dimen name = "button1_width_large">315dip</dimen>

<!-- XLarge Dimensions = "Medium Dimensions * 2" For Example: 210*1.5 = 420 -->
<dimen name = "button1_width_xLarge">420dip</dimen>

并按如下所示在Layouts(普通/中级)中使用它们:

And use them in your Layouts (Normal/Medium) like this:

<Button
    android:id="@+id/button1"
    android:layout_width="@dimen/button1_width_medium"
    android:layout_height="210dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/animation0" />

要转换尺寸,请使用以下值:

For converting the dimensions use the following Values:

0.75 - ldpi  (small)   //mdpi dimens *0.75
1.0  - mdpi  (normal)  //First create these dimensions
1.5  - hdpi  (large)   //mdpi dimens *1.5
2.0  - xhdpi (xLarge)  //mdpi dimens *2.0

您还需要在res文件夹中为所有设备创建不同的Layouts Folders,并相应地使用尺寸.

You also need to create different Layouts Folders in your res folder for all devices and use the dimensions accordingly.

通用布局文件夹( Android开发指南):

Generic Layout Folders (Android Dev Guide) :

res/layout-small
res/layout-normal
res/layout-large
res/layout-xlarge


完成Normal/Medium Layouts的制作后,请执行以下步骤:


After you are done with making your Normal/Medium Layouts follow these steps:

  1. 将普通尺寸转换为其他屏幕尺寸.
  2. 将标准布局" xml文件复制到其他文件夹中.
  3. 根据您所在的文件夹更改所用尺寸的后缀.
  4. 在可绘制文件夹中调整图像资源的大小(宽度和高度-与转换调暗度时使用的技术相同),然后将它们放在各自的可绘制文件夹(drawable-ldpi,drawable-mdpi,drawable-hdpi,drawable-xdpi等).

然后,您的布局应在每台设备上都能正确定位.
我希望这会有所帮助.

Then your Layouts should work on every device with correct positioning.
I hope this helps.

这篇关于适用于所有设备的Android XML布局(小型/普通/大型/XLarge等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆