Android XML格式布局

Android XML格式布局,android,android-layout,android-linearlayout,android-relativelayout,Android,Android Layout,Android Linearlayout,Android Relativelayout,是否有一种布局可以让您在xml上格式化内容,这样您就可以将按钮和文本放置在您想要的任何位置,而不会使它们相对于其他框进行更改?当我尝试使用relativelayout时,这是一场噩梦,因为移动一个盒子会使其他所有东西也移动。当我尝试linearlayout时,(我可能用错了,但我尝试了两个小时),我无法将盒子准确地移动到我想要的地方,它会朝大致方向移动,但我无法微调放置 我试着把按钮放在我想放的任何地方,每个按钮彼此独立 请注意,市场上有大量不同的Android设备,具有不同的屏幕大小和分辨率

是否有一种布局可以让您在xml上格式化内容,这样您就可以将按钮和文本放置在您想要的任何位置,而不会使它们相对于其他框进行更改?当我尝试使用relativelayout时,这是一场噩梦,因为移动一个盒子会使其他所有东西也移动。当我尝试linearlayout时,(我可能用错了,但我尝试了两个小时),我无法将盒子准确地移动到我想要的地方,它会朝大致方向移动,但我无法微调放置

我试着把按钮放在我想放的任何地方,每个按钮彼此独立

请注意,市场上有大量不同的Android设备,具有不同的屏幕大小和分辨率

您有责任调整应用程序,使其在所有应用程序中都能完美运行

在你的情况下,我强烈建议你习惯于
RelativeLayout

不难理解它是如何工作的

您可以在创建屏幕之前规划用户界面和用户体验的外观,因此,如果将来需要进行任何更改,您可以将工作量降至最低

你可以使用纸和笔,或者使用一些设计工具,比如你可以使用一个。此布局允许您指定布局中所有小部件的X/Y坐标


然而,这种布局是最不灵活的布局之一。您的屏幕布局将直接与为其创建的设备的分辨率相关联。当您的应用程序需要在各种手机/平板电脑等上运行时,线性布局和相对布局更加灵活。

根据您在评论中描述的所需布局,我建议如下:

使用
RelativeLayout
GridLayout
,在其上放置所需的最大数量的按钮,然后使用根
视图
UI类的“可见性”XML选项(类方法为)动态显示或隐藏按钮。按钮不会显示,但会保持其位置。

这是我使用的布局(1singleRelativeLayout)


为了得到这种类似“网格”的设计

如果没有优化,我应该为按钮公共属性使用样式

再添加4个按钮不是什么大问题。
关键字是相对性

请注意,我是如何设置正确的属性以将所有按钮与中心按钮对齐的

无论如何,你看,这些按钮中有5个数字。

足够满足您的1、2或3位数请求。

您可以使用两者的组合:)如何合并它们?在xml上,我会有两个单独的linearlayout和relativelayout部分,还是会嵌套?
所有内容都会相对移动,因此如果我移动一个内容,其他所有内容都会因相对布局而更改。
否。所有项目都必须相对于容器和/或在它们之间放置<代码>当我使用linearlayout时,按钮只能放置在网格中否。所有项目都会随之放置。水平或垂直(默认值),如您所指定。因此,您不能将其放置在任何位置,只能将某些点放置在不可见的网格(如3x3正方形)上?您根本不应该将项目放置在精确的像素位置,因为布局只能在特定的显示分辨率下工作。任何屏幕布局都应根据不同的屏幕尺寸灵活设计!
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    >
    <Button
        android:id="@+id/btnCenter"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:background="@drawable/ball"
        android:text="Btn 0"
        android:textColor="@android:color/white"
        android:textSize="24sp"
        android:textStyle="bold"
    />
    <Button
        android:id="@+id/btnTop"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_above="@id/btnCenter"
        android:layout_alignLeft="@id/btnCenter"
        android:background="@drawable/ball"
        android:text="Btn 1"
        android:textColor="@android:color/white"
        android:textSize="24sp"
        android:textStyle="bold"
    />
    <Button
        android:id="@+id/btnLeft"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignTop="@id/btnCenter"
        android:layout_toLeftOf="@id/btnCenter"
        android:background="@drawable/ball"
        android:text="Btn 2"
        android:textColor="@android:color/white"
        android:textSize="24sp"
        android:textStyle="bold"
    />
    <Button
        android:id="@+id/btnBottom"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_below="@id/btnCenter"
        android:layout_alignLeft="@id/btnCenter"
        android:background="@drawable/ball"
        android:text="Btn 3"
        android:textColor="@android:color/white"
        android:textSize="24sp"
        android:textStyle="bold"
    />
    <Button
        android:id="@+id/btnRight"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignTop="@id/btnCenter"
        android:layout_toRightOf="@id/btnCenter"
        android:background="@drawable/ball"
        android:text="Btn 4"
        android:textColor="@android:color/white"
        android:textSize="24sp"
        android:textStyle="bold"
    />
</RelativeLayout>