Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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_User Interface - Fatal编程技术网

Android 如何使按钮在中间对齐?安卓工作室

Android 如何使按钮在中间对齐?安卓工作室,android,user-interface,Android,User Interface,如何使这些按钮保持在中间对齐?这是我的界面 下面是我的代码。我不应该使用保证金。有没有办法让它在中间对齐 <LinearLayout android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:gravity="center|bottom" android:orientat

如何使这些按钮保持在中间对齐?这是我的界面

下面是我的代码。我不应该使用保证金。有没有办法让它在中间对齐

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

这已经处于底部。只有我需要的才能让它看起来更好。请帮忙。提前谢谢

更新

如何让空白消失


您可以使用android:stretchColumns=“*”并将按钮宽度设置为
android:layout\u width=“wrap\u content”


我建议您使用RelativeLayout而不是TableLayout。 像这样

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

->重力:中心和宽度:匹配父项
->正确:正确

只需添加替换
android:layout\u width=“250dp”
按钮代码即可

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

它将以固定宽度将所有
按钮居中。

正如我们所知
表格行
线性布局
的子项,因此可以使用线性布局属性
android:layou 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>

您需要使用
相对布局
线性布局
。我使用了
LinearLayout
。使用此代码对齐按钮

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

试试这个:

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


您只粘贴了按钮的代码。可以发布完整代码吗?我想输入完整代码,但它一直说我输入了大部分代码@Sabarinathan@Sabarinathan实际上是这样enough@LeonZaii你真的只想使用TableLayout吗?是的。它看起来更像@mobiledevow,像这样也可以。但是按钮看起来有点小。@LeonZaii身高小吗?。如果是这种情况,请使用高度作为“匹配父项”,而不是“包装内容”,好的。谢谢你帮助我@Umair@LeonZaii很乐意帮忙。快乐编码:)你能帮我检查一下最新的问题吗@乌米尔
<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>