简体   繁体   English

如何使按钮在中间对齐? Android Studio

[英]how to make buttons align at the middle? android studio

how do I make those buttons stay align at the middle? 如何使这些按钮在中间对齐? this is my interface. 这是我的界面。

在此处输入图片说明

here is my following code. 这是我的以下代码。 I shouldn't use margin. 我不应该使用保证金。 is there any way to make it align at the middle? 有什么方法可以使其在中间对齐?

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center|bottom"
        android:orientation="horizontal">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TableRow>

                <Button
                    android:id="@+id/Db_New"
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimary"
                    android:text="NEW" />

                <Button

                    android:id="@+id/Db_Save"
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimary"
                    android:enabled="false"
                    android:text="SAVE" />
            </TableRow>

            <TableRow>

                <Button
                    android:id="@+id/Db_Print"
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimary"
                    android:text="PRINT" />

                <Button
                    android:id="@+id/Db_Back"
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimary"
                    android:text="BACK" />
            </TableRow>
        </TableLayout>
    </LinearLayout>

this is already staying in bottom. 这已经停留在底部。 Only What I need it make it to look better. 只有我需要它,它才能看起来更好。 please help. 请帮忙。 Thanks in advance 提前致谢

UPDATE UPDATE

How to make the white space gone? 如何使空白消失?

在此处输入图片说明

You need to use either RelativeLayout or LinearLayout . 您需要使用RelativeLayoutLinearLayout I have used LinearLayout . 我使用了LinearLayout Use this code to align your buttons. 使用此代码来对齐您的按钮。

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center|bottom"
    android:orientation="horizontal"
    android:baselineAligned="false">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center"
        android:orientation="vertical">

        <Button
            android:id="@+id/Db_New"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="NEW" />

        <Button
            android:id="@+id/Db_Print"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="PRINT" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center"
        android:orientation="vertical">

        <Button

            android:id="@+id/Db_Save"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:enabled="false"
            android:text="SAVE" />

        <Button
            android:id="@+id/Db_Back"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="BACK" />

    </LinearLayout>
</LinearLayout>

You can use android:stretchColumns="*" and set your buttons width to android:layout_width="wrap_content" 您可以使用android:stretchColumns="*"并将按钮宽度设置为android:layout_width="wrap_content"

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*">

    <TableRow>

        <Button
            android:id="@+id/Db_New"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="NEW" />

        <Button

            android:id="@+id/Db_Save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:enabled="false"
            android:text="SAVE" />
    </TableRow>

    <TableRow>

        <Button
            android:id="@+id/Db_Print"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="PRINT" />

        <Button
            android:id="@+id/Db_Back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="BACK" />
    </TableRow>
</TableLayout>

I recommend you to use RelativeLayout instead of TableLayout. 我建议您使用RelativeLayout而不是TableLayout。 Like this. 像这样。

<RelativeLayout>
  <newButton /> -> gravity:center and width:match_parent
  <saveButton /> -> alignParentsRight:true
</RelativeLayout>

Just add replace android:layout_width="250dp" button code with 只需添加将android:layout_width="250dp"按钮代码替换为

android:layout_width="0dp"
android:layout_weight="1"

It will center all the Button with fixed width. 它将以固定宽度居中所有Button

As we know TableRow is a child of LinearLayout , So it is possible to do what you want using LinearLayout attribute android:layout_weight so we should have sth like: 我们知道TableRowLinearLayout的子级,因此可以使用LinearLayout属性android:layout_weight来做您想做的事情,因此我们应该像这样:

<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:weightSum="2">

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


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

    <TableRow
        android:weightSum="2">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="print"/>


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

Try this: 尝试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center|bottom"
    android:orientation="vertical">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            >

            <Button
                android:id="@+id/Db_New"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:text="NEW" 
                android:layout_weight="0.5"/>

            <Button

                android:id="@+id/Db_Save"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:enabled="false"
                android:text="SAVE"
                android:layout_weight="0.5"/>
        </TableRow>

        <TableRow>

            <Button
                android:id="@+id/Db_Print"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:text="PRINT"
                android:layout_weight="0.5"/>

            <Button
                android:id="@+id/Db_Back"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:text="BACK"
                android:layout_weight="0.5"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

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

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