【问题标题】:Android XML Layout for all Devices (Small/Normal/Large/XLarge, etc)适用于所有设备的 Android XML 布局(Small/Normal/Large/XLarge 等)
【发布时间】:2017-01-05 16:09:27
【问题描述】:

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

我该怎么做?

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

这是我为 Normal/Medium 布局创建的 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>

【问题讨论】:

    标签: android android-xml


    【解决方案1】:

    您需要将所有WidthsHeightsPaddingsMargins 等放入/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>
    

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

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

    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,并相应地使用尺寸。

    通用布局文件夹 (Android Dev Guide):

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


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

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

    然后,您的布局应该可以在具有正确定位的每台设备上运行。
    我希望这会有所帮助。

    【讨论】:

    • 我认为您已经为 Normal Size 创建了布局,您只需像这样将它们相乘即可获得所需的尺寸:Small Dimensions = Normal dimens *0.75 Large Dimensions = Normal dimens *1.5 xlarge Dimensions = Normal dimens *2.0
    • 我已经创建了 res/layout-small,res/layout-normal,res/layout-large, res/layout-xlarge,但是我该如何使用它(Small Dimensions = Normal dimens *0.75)在xml中?
    • 您必须自己计算这些值并将它们放入dimens文件中,检查上面dimens.xml :标题下的答案,medium value is 210dip然后small value is 157.5dip,等于(210* 0.75)等等。
    【解决方案2】:

    因此您需要创建不同的文件夹并维护这些文件夹中的所有 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
    

    更多信息请参考link

    【讨论】:

    • 兄弟我用过,但不是我想要的完全一样的位置。请帮帮我兄弟。
    【解决方案3】:

    你还需要在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"/>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2021-12-15
    相关资源
    最近更新 更多