【问题标题】:how to make buttons align at the middle? android studio如何使按钮在中间对齐?安卓工作室
【发布时间】:2018-10-01 06:45:17
【问题描述】:

如何让这些按钮居中对齐?这是我的界面。

这是我的以下代码。我不应该使用保证金。有什么办法让它居中对齐吗?

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

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

更新

如何让空白消失?

【问题讨论】:

  • 您只粘贴了按钮的代码。可以发布完整代码吗?。
  • 我想放完整的代码,但它一直说我放的主要是代码...@Sabarinathan
  • @Sabarinathan 其实这就够了
  • @LeonZaii 你真的只想使用 TableLayout 吗?
  • 是的。它看起来更对齐...@mobiledev

标签: android user-interface


【解决方案1】:

您需要使用RelativeLayoutLinearLayout。我用过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>

【讨论】:

  • 哇,这样也行。但是按钮看起来有点小。
  • @LeonZaii 身高很小?如果是这种情况,请使用高度作为“match_parent”而不是“wrap_content”
  • 好的。感谢您帮助我@Umair
  • @LeonZaii 很乐意为您提供帮助。快乐编码:)
  • 你能帮我检查一下最新的问题吗? @Umair
【解决方案2】:

您可以使用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>

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      只需添加替换android:layout_width="250dp"按钮代码为

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

      它将以固定宽度居中所有Button

      【讨论】:

        【解决方案5】:

        我们知道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>
        

        【讨论】:

          【解决方案6】:

          试试这个:

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

          【讨论】:

            猜你喜欢
            • 2018-01-08
            • 1970-01-01
            • 2018-01-29
            • 2017-02-09
            • 1970-01-01
            • 1970-01-01
            • 2022-01-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多