适用于所有设备的 Android XML 布局(小型/普通/大型/超大型等)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21361285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:25:20  来源:igfitidea点击:

Android XML Layout for all Devices (Small/Normal/Large/XLarge, etc)

androidandroid-xml

提问by Mohammad Rajob

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

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

How can I do this?

我怎样才能做到这一点?

Here is the XML output should be look like:

XML 输出应如下所示:

enter image description here

在此处输入图片说明

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

这是我为普通/中等布局创建的 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>

回答by Salman Khakwani

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

你需要把所有的WidthsHeightsPaddingsMargins,等中/res/values/dimens.xml像这样的文件:

dimens.xml:

尺寸.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>

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

并在您的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" />

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

You also need to create different Layouts Foldersin your resfolder for all devices and use the dimensions accordingly.

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

Generic Layout Folders (Android Dev Guide) :

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

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


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


完成后,请Normal/Medium Layouts按照以下步骤操作:

  1. Convert the Normal Dimensions for other Screen Sizes.
  2. Copy your Normal Layout xml files in to other Folders.
  3. Change the suffix of the dimensions used according to the folder that you are in.
  4. Resize the Image Resources in your drawable folder (Width and Height - Same technique as we used for converting the dimens) and put them in their respective drawable folder (drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xdpi and so on).
  1. 转换其他屏幕尺寸的正常尺寸。
  2. 将您的 Normal Layout 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.

然后你的布局应该在每台设备上都能正确定位。
我希望这有帮助。

回答by Shylendra Madda

So you need to create different folders and maintain all xml in those folders.

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

The following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for medium, high, and extra high density screens.

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

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

For more info refer this link

有关更多信息,请参阅此链接

回答by Strict Way

You also need to support different screens in manifast xml file Open "AndroidManifest" and add the following after android versionName.

您还需要在manifast xml文件中支持不同的屏幕打开“AndroidManifest”并在android versionName之后添加以下内容。

android:versionName="1.0" >

<supports-screens android:smallScreens="true"

                android:normalScreens="true"

                android:largeScreens="true"

                android:xlargeScreens="true"

                android:anyDensity="true"

                android:resizeable="true"/>