简体   繁体   English

如何使6个按钮对齐且大小相同?

[英]How do I make 6 buttons aligned and same size?

I am trying to use the design tab in Android Studio to design my XML for my homescreen, however when i try and drag a button it realigned everything on the layout, eg like in the image below: 我正在尝试使用Android Studio中的“设计”选项卡为我的主屏幕设计XML,但是当我尝试拖动按钮时,它会重新排列布局中的所有内容,例如下图所示: 我的混乱XML

I want to arrange it so the buttons are a uniform size and aligned in a 2x3 table like the following 我要对其进行排列,以使按钮尺寸统一并在2x3表格中对齐,如下所示

ab b

cd 光盘

ef ef

I would like the image to remain in the same place as it is currently, with the text underneath also not to move. 我希望图像保持在当前位置,并且下面的文字也不要移动。

Current XML: 当前的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>

Cheers in advance! 提前加油!

Added tablerows, now all is aligned to the left hand side: 添加了表格行,现在所有行都对齐到左侧:

    <?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>

Try this and let me know 试试这个,让我知道

<?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>

Here is what it look without assets 这是没有资产的外观

在此处输入图片说明

Comes to this(At Paul's , with assets): 得出以下结论(在Paul的资产处): 结果1

This might work 这可能有效

<?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>

You can use LinearLayouts inside another LinearLayout just like this: 您可以在另一个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>

You can add the ImageView and TextView before the first LinearLayout that keeps the first two Buttons. 您可以在第一个LinearLayout之前添加ImageView和TextView,以保留前两个Button。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM