Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何为具有不同纵横比的屏幕大小布局按钮_Android_Android Layout_User Interface_Button_Aspect Ratio - Fatal编程技术网

Android 如何为具有不同纵横比的屏幕大小布局按钮

Android 如何为具有不同纵横比的屏幕大小布局按钮,android,android-layout,user-interface,button,aspect-ratio,Android,Android Layout,User Interface,Button,Aspect Ratio,我试图在一个用作多种设备/屏幕大小背景的图像上放置按钮。如果我在宽高比为9:16的设备(例如Galaxy Nexus或Nexus 5)的relativeLayout中定义按钮,使按钮与图像上的特定点对齐,则它不会与宽高比为3:5的设备(例如Nexus S)上的相同图像对齐。在我的相对布局中,第一个按钮与屏幕的左侧和底部对齐,并有一个偏移量(例如,android:layout_marginLeft=“32dp”和android:layout_marginBottom=“150dp”)。所有其他按钮

我试图在一个用作多种设备/屏幕大小背景的图像上放置按钮。如果我在宽高比为9:16的设备(例如Galaxy Nexus或Nexus 5)的relativeLayout中定义按钮,使按钮与图像上的特定点对齐,则它不会与宽高比为3:5的设备(例如Nexus S)上的相同图像对齐。在我的相对布局中,第一个按钮与屏幕的左侧和底部对齐,并有一个偏移量(例如,android:layout_marginLeft=“32dp”和android:layout_marginBottom=“150dp”)。所有其他按钮都与此按钮对齐

我花了好几天的时间试图通过实验和搜索教程来解决这个问题,但似乎什么都不管用

下图是针对Nexus5的。如您所见,按钮与背景对齐:

下图为Nexus S。正如您所见,按钮与背景偏移

编辑: 我显然遗漏了一些关键概念。对于hdpi、xhdpi和xxhdpi的每个屏幕密度,我都有单独的目录。但是,根据我所附的两张图片,我似乎需要为尺寸为480x800、720x1280和1080x1920的屏幕设置一个单独的布局。720x1280的屏幕似乎需要与1080x1920屏幕相同的布局,因为Nexus 5和Galaxy Nexus具有相同的结果(如第一张图所示)。为了创建一个单独的布局,我尝试使用layout-small、layout、layout-large等(这是不推荐的方法),以及layout-sw480dp和layout-sw720dp。然后,调整第一个按钮的layout_marginLeft和layout_margin_底部以获得适当的屏幕大小。这两种方法都不起作用

以下是第一幅图像的当前布局内容:

<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"
    android:paddingLeft="15dp"
    android:paddingBottom="5dp"
    tools:context="com.myCompany.myApp.MainActivity"
    android:background="@drawable/android_home_screen">

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:text="Channel 1"
    android:id="@+id/channel1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:textColor="#FFFFFF"
    android:layout_marginBottom="145dp"
    android:layout_marginLeft="17dp"
    android:lines="2" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:text="Channel 3"
    android:id="@+id/channel3"
    android:textColor="#FFFFFF"
    android:layout_marginLeft="0dp"
    android:layout_toRightOf="@+id/channel1"
    android:layout_alignTop="@+id/channel1"
    android:lines="2" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:text="Channel 5"
    android:id="@+id/channel5"
    android:textColor="#FFFFFF"
    android:layout_marginLeft="0dp"
    android:layout_toRightOf="@+id/channel3"
    android:layout_alignTop="@+id/channel3"
    android:lines="2" />

...

</RelativeLayout>

...
请帮助我了解如何在图像的特定点上放置按钮,而不考虑屏幕大小/纵横比/屏幕密度。具体而言,请提供一个示例,说明如何定义在屏幕尺寸为480x800和720x1280的背景图像的同一可视点上显示的按钮

以下是屏幕背景的完整图像。我试图把按钮放在一个薄的金色轮廓区域的顶部。hdpi、xhdpi和xxhdpi的每个可绘制目录中都有一个。hdpi图像为480x693像素,xhdpi图像为720x1040像素,xxhdpi图像为1080x1559像素,分辨率分别为213.34 ppi、320 ppi和480 ppi。这些分辨率和尺寸使图像在点上具有相同的图像大小(宽度和高度)。

您可以使用线性布局的“权重”属性管理布局,它将自动调整屏幕

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="10dp"
        android:background="#fff"
        android:orientation="vertical"
        android:padding="10dp" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="3" >

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:orientation="horizontal"
            android:weightSum="1" >

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".1" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".4" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".4" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".1" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>


首先,避免使用32dp或150dp等特定对齐方式。请始终尝试使用wrap\u content或fill\u parent。对于所有设备支持,请阅读此链接,希望它可以帮助您和@MycareerId-我使用的是安卓Studio,它会自动将这些特定对齐号码放入。如果没有特定的对齐编号,我无法让第一个按钮停留在图像上的特定点上。您建议使用什么方法将按钮锚定到图像上的某个点上。谢谢你的链接。我已经尝试过不同的布局目录,但并没有成功。我将详细介绍堆栈溢出链接,看看是否还有其他示例可以尝试。@MycareerId-不幸的是,这些引用都不能解决我的问题。根据我所附的两张图片,我似乎需要为尺寸为480x800和720x1280的屏幕设置一个单独的布局。由于Nexus 5和Galaxy Nexus具有相同的结果,因此720x1280的屏幕似乎需要与1080x1920的屏幕相同的布局。所以,我尝试使用layout-small、layout、layout-large等,以及layout-sw48dp和layout-sw720dp。这两种方法都不起作用。事实上,它们会使仿真器崩溃。所以,我遗漏了一些关键信息。帮助只需将不同大小的图像放在不同的可绘制文件夹(drawablemdpi drawablehdpi…)中,并为不同的文件夹制作不同的布局即可screen@MycareerId-谢谢。我解释这件事一定做得不好。我认为你的建议正是我正在做的。但是我使用的技术不起作用。我在不同的可绘制文件夹中有不同大小的图像。然后我为每个图像创建不同的布局。具体来说,我创建了以下文件夹,每个文件夹中都有唯一的xml文件。文件夹是布局、小布局、大布局。我还尝试了名为layout-sw480dp和layout-sw720dp的文件夹。后来的模拟器崩溃了,前者只使用了9:16纵横比的布局。我这样做对了吗?谢谢,但我需要使用相对布局,因为按钮并非都整齐地排列在图像顶部的行和列中。共有6行,所有行都具有不同大小的按钮和文本视图。大多数行开始于不同的水平位置和不同的垂直位置。您可以在相对布局内创建多个线性布局,并可以根据需要管理视图。在相对布局内,您可以使用相对布局的layout:below属性在相对布局内水平对齐线性布局,我想出了如何在相对布局中包含多个线性布局。虽然从水平的角度来看这似乎有帮助,但从垂直的角度来看,它仍然不能解决我的问题。如图所示,当纵横比从3:5更改为9:16时,按钮不会对齐。除非有其他方法,否则使用线性布局仍然需要使用paddingBottom、paddingRight和paddingLeft来放置