【问题标题】:How do I make 6 buttons aligned and same size?如何使 6 个按钮对齐且大小相同?
【发布时间】:2014-11-11 15:00:25
【问题描述】:

我正在尝试使用 Android Studio 中的设计选项卡为我的主屏幕设计我的 XML,但是当我尝试拖动一个按钮时,它会重新对齐布局上的所有内容,如下图所示:

我想安排它,使按钮大小一致,并在 2x3 表格中对齐,如下所示

a b

c d

e f

我希望图像保持在当前位置,下面的文本也不要移动。

当前 XML:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/amityapplogo"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/welcomeadmin"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/mydetails"
        android:id="@+id/mydetailsbutton"
        android:layout_gravity="center_horizontal"
        android:clickable="false"
        android:layout_toStartOf="@+id/myhealthbutton"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/policydetails"
        android:id="@+id/policydetailsbutton"
        android:layout_gravity="center_horizontal"
        android:layout_alignTop="@+id/mydetailsbutton"
        android:layout_toRightOf="@+id/findaproviderbutton"
        android:layout_toEndOf="@+id/findaproviderbutton" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/myhealth"
        android:id="@+id/myhealthbutton"
        android:layout_gravity="center_horizontal"
        android:layout_toStartOf="@+id/contactusbutton"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/findaprovider"
        android:id="@+id/findaproviderbutton"
        android:layout_gravity="center_horizontal"
        android:layout_toStartOf="@+id/myhealthbutton"
        android:layout_below="@+id/visitourwebsitebutton"
        android:layout_alignLeft="@+id/imageView"
        android:layout_alignStart="@+id/imageView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/contactus"
        android:id="@+id/contactusbutton"
        android:layout_gravity="center_horizontal"
        android:layout_toEndOf="@+id/textView"
        android:layout_below="@+id/visitourwebsitebutton"
        android:layout_alignRight="@+id/imageView"
        android:layout_alignEnd="@+id/imageView"
        android:layout_marginTop="40dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/visitourwebsite"
        android:id="@+id/visitourwebsitebutton"
        android:layout_gravity="center_horizontal"
        android:layout_toEndOf="@+id/myhealthbutton"
        android:layout_alignBaseline="@+id/myhealthbutton"
        android:layout_alignBottom="@+id/myhealthbutton"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

提前干杯!

添加了 tablerows,现在所有都对齐到左侧:

    <?xml version="1.0" encoding="utf-8"?>
<TableLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="fill_horizontal"
    android:id="@+id/">

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:src="@drawable/amityapplogo" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/welcomeadmin"
            android:id="@+id/textView"
            android:layout_gravity="center_horizontal" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_column="1"
            android:orientation="horizontal"
            android:id="@+id/">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/mydetails"
                android:id="@+id/mydetailsbutton"
                android:layout_column="0" />
        </TableRow>
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/policydetails"
            android:id="@+id/policydetailsbutton" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/myhealth"
            android:id="@+id/myhealthbutton" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/findaprovider"
            android:id="@+id/findaproviderbutton" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/visitourwebsite"
            android:id="@+id/visitourwebsitebutton" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/contactus"
            android:id="@+id/contactusbutton"
            android:layout_gravity="center_vertical" />
    </TableRow>

</TableLayout>

【问题讨论】:

  • 制作表格布局,其中表格行包含按钮。
  • 您需要表格格式,还是只需要按这种方式排列的按钮(没有表格轮廓)??
  • 不管怎样,只要它能在不同的设备上扩展@AkshitRewari
  • GridView 或 GridLayout 可能会有所帮助。
  • @Funkystein 好喊,我用它然后变形为 tablerows。将很快添加编辑。

标签: android xml user-interface


【解决方案1】:

试试这个,让我知道

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/amityapplogo"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/welcomeadmin"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mydetails"
    android:id="@+id/mydetailsbutton"
    android:clickable="false"
    android:layout_marginTop="36dp"
    android:layout_below="@+id/findaproviderbutton"
    android:layout_alignRight="@+id/myhealthbutton"
    android:layout_alignEnd="@+id/myhealthbutton" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/policydetails"
    android:id="@+id/policydetailsbutton"
    android:layout_toStartOf="@+id/mydetailsbutton"
    android:layout_above="@+id/findaproviderbutton"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="36dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/myhealth"
    android:id="@+id/myhealthbutton"
    android:layout_toEndOf="@+id/imageView"
    android:layout_below="@+id/policydetailsbutton"
    android:layout_alignLeft="@+id/imageView"
    android:layout_alignStart="@+id/imageView" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/findaprovider"
    android:id="@+id/findaproviderbutton"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/mydetailsbutton"
    android:layout_toStartOf="@+id/mydetailsbutton" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/contactus"
    android:id="@+id/contactusbutton"

    android:layout_toEndOf="@+id/imageView"
    android:layout_alignBottom="@+id/policydetailsbutton"
    android:layout_alignLeft="@+id/myhealthbutton"
    android:layout_alignStart="@+id/myhealthbutton" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/visitourwebsite"
    android:id="@+id/visitourwebsitebutton"
    android:layout_toEndOf="@+id/policydetailsbutton"
    android:layout_alignBottom="@+id/mydetailsbutton"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/contactusbutton"
    android:layout_toStartOf="@+id/contactusbutton" />

</RelativeLayout>

这是没有资产的样子

来到这个(在保罗的,有资产):

【讨论】:

  • 我添加了照片,看看。全部向左对齐。
  • 那是因为我在编辑时无法查看图片资源和 strings.xml。您所要做的就是将联系我们拉到右侧并将按钮的宽度调整到左侧。(其余应相应对齐)
  • 抱歉,如果浪费您的时间,但在使用 tablerows 之后,我更喜欢 hte 整体外观。唯一的问题是我无法将其对齐在中心。现在将发布编辑。
  • 这样更简单,在Table中使用android:layout_gravity="center_horizo​​ntal"
  • 谢谢,layout_gravity 成功了。感谢您的帮助。
【解决方案2】:

这可能有效

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:text="ggggg"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/mydetailsbutton"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/textView"
    android:layout_marginTop="42dp"
    android:text="Details" />

<Button
    android:id="@+id/policydetailsbutton"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/mydetailsbutton"
    android:layout_alignTop="@id/mydetailsbutton"
    android:layout_alignBottom="@id/mydetailsbutton"
    android:text="eeeee" />

<Button
    android:id="@+id/myhealthbutton"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@id/mydetailsbutton"
    android:layout_alignLeft="@id/mydetailsbutton"
    android:layout_alignRight="@id/mydetailsbutton"
    android:text="ddddd" />

<Button
    android:id="@+id/findaproviderbutton"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@id/mydetailsbutton"
    android:layout_alignLeft="@id/policydetailsbutton"
    android:layout_alignRight="@id/policydetailsbutton"
    android:text="ccccc" />

<Button
    android:id="@+id/contactusbutton"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@id/myhealthbutton"
    android:layout_alignLeft="@id/mydetailsbutton"
    android:layout_alignRight="@id/mydetailsbutton"
    android:text="bbbbb" />

<Button
    android:id="@+id/visitourwebsitebutton"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@id/myhealthbutton"
    android:layout_alignLeft="@id/policydetailsbutton"
    android:layout_alignRight="@id/policydetailsbutton"
    android:text="aaaaa" />

</RelativeLayout>

【讨论】:

  • 重要的是为顶部按钮提供自定义宽度,它们应该具有相同的宽度
  • 自定义宽度将为顶部按钮(以及所有其他按钮)设置一个固定宽度,该宽度在所有屏幕尺寸上都将保持不变。相反,我们应该指定布局边距并指定其他按钮以匹配顶部按钮
【解决方案3】:

您可以像这样在另一个 LinearLayout 中使用 LinearLayouts:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:text="Button1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:text="Button2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:text="Button3" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:text="Button4" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:text="Button5" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:text="Button6" />
    </LinearLayout>

</LinearLayout>

您可以在保留前两个按钮的第一个 LinearLayout 之前添加 ImageView 和 TextView。

【讨论】:

  • 其余项目仍需要相对布局。因此需要一个嵌套布局,这里不需要。没有线性布局更容易实现
猜你喜欢
  • 1970-01-01
  • 2018-05-31
  • 2020-12-26
  • 2015-11-15
  • 2016-11-24
  • 2020-03-17
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
相关资源
最近更新 更多