所有设备的Android XML布局(小/普通/大/ XLarge等)

Moh*_*job 8 android android-xml

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

我怎样才能做到这一点?

这里的XML输出应该是这样的:

在此输入图像描述

这是我为普通/中等布局创建的XML文件:

<?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>
Run Code Online (Sandbox Code Playgroud)

Sal*_*ani 23

你需要把所有的Widths,Heights,Paddings,Margins,等在/res/values/dimens.xml像这样的文件:

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>
Run Code Online (Sandbox Code Playgroud)

并在你的Layouts(普通/中等)中使用它们,如下所示:

<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" />
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

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

res/layout-small
res/layout-normal
res/layout-large
res/layout-xlarge
Run Code Online (Sandbox Code Playgroud)


完成Normal/Medium Layouts以下步骤后:

  1. 转换其他屏幕尺寸的正常尺寸.
  2. 将Normal Layout xml文件复制到其他文件夹中.
  3. 根据您所在的文件夹更改使用的尺寸的后缀.
  4. 调整可绘制文件夹中的图像资源(宽度和高度 - 与我们用于转换尺寸的技术相同)并将它们放在各自的可绘制文件夹中(drawable-ldpi,drawable-mdpi,drawable-hdpi,drawable-xdpi等等) ).

然后你的布局应该适用于每个设备正确定位.
我希望这有帮助.


Shy*_*dda 1

因此,您需要创建不同的文件夹并维护这些文件夹中的所有 xml。

以下是应用程序中的资源目录列表,该应用程序为不同的屏幕尺寸提供不同的布局设计,并为中、高和超高密度屏幕提供不同的位图可绘制对象。

res/layout/my_layout.xml             // layout for normal screen size ("default")
res/layout-small/my_layout.xml       // layout for small screen size
res/layout-large/my_layout.xml       // layout for large screen size
res/layout-xlarge/my_layout.xml      // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息,请参阅此链接