【问题标题】:Align buttons inside a TableRow对齐 TableRow 内的按钮
【发布时间】:2011-04-21 20:20:03
【问题描述】:

我正在开发一个 Android 应用程序。

我有这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="200px"
  android:orientation="vertical">
  <TextView
        android:id="@+id/gameTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="left"
        android:layout_margin="5px"
        android:text="aaaaa"/>
  <TextView
        android:id="@+id/gameDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="left"
        android:layout_margin="5px"
        android:typeface="sans"
        android:textSize="16sp"
        android:textStyle="bold"
        android:text="dddddd"/>
  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ButtonsTable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <Button
                android:text="@string/yes"
                android:id="@+id/playGame"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <Button
                android:text="@string/no"
                android:id="@+id/noPlayGame"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

我想把playGame按钮放在中间的左边,把noPlayGame按钮放在中间的右边。

现在,两者都显示在 TableRow 的左侧。

我该怎么做?

谢谢。

编辑:我添加了完整的布局和 Ashay 建议的修改。
第二次编辑
解决方案
我已将以下内容添加到 TableLayoutandroid:stretchColumns="0,1"。现在按钮居中对齐,但它们填满了整个列!!!

【问题讨论】:

  • 可能值得发布您自己的答案并将其标记为已回答,这样就不会一直出现在未回答的问题列表中。
  • 我不喜欢回答我自己的问题。
  • 就是这样。谢谢。您的解决方案工作。

标签: android tablelayout


【解决方案1】:

我希望这会有所帮助:

<TableRow
    android:id="@+id/tableRow1"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/stringtxt1"></Button>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:text="@string/stringtxt2"
        android:layout_height="wrap_content"
        android:layout_gravity="center|center_horizontal"></Button>
</TableRow>

【讨论】:

    【解决方案2】:

    我发布了我的代码,希望能帮助其他用户解决他们的问题(中心内容)。我想在表格行中居中两个按钮,这是我的代码,它解决了这个问题:

    我们使用逗号分隔的列表在prop android:strechColumns="a,b.." 中指明哪些列将被拉伸

          <TableLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/buttonGroup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="0,1" > 
    
            <TableRow>
                <Button
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/button1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/button1Name" 
                    android:layout_gravity="right"/>
    
                <Button
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/button2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/button2Name" 
                    android:layout_gravity="left"/>
            </TableRow>
        </TableLayout>
    

    我希望它会有所帮助。对不起我的英语:/

    【讨论】:

      【解决方案3】:

      用于将单个按钮置于单行的中心:

      <TableRow android:id="@+id/rowNameHere"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:gravity="center">
          <Button android:id="@+id/btnDoStuff"
                  android:text="Do Stuff" />
      </TableRow>
      

      在查看了上述答案后,我从每个答案中提取了一些东西,这对我有用。

      【讨论】:

      • 谢谢!! android:gravity="end" 对我有用,可以将按钮对齐到表格行的右侧。
      • 很高兴能帮上忙。我很震惊,这么多年后这个答案仍然有意义。
      【解决方案4】:

      我添加了两个虚拟文本视图以使我的按钮居中对齐。

      <TableRow>
          <TextView 
              android:text=""
              android:layout_weight=".25" 
              android:layout_width="0dip"         
              />
          <Button 
              android:id="@+id/buttonSignIn" 
              android:text="Log In"
              android:layout_height="wrap_content"
              android:layout_weight=".50" 
              android:layout_width="0dip" 
              />
          <TextView 
              android:text=""
              android:layout_weight=".25" 
              android:layout_width="0dip"         
          />
      </TableRow>
      

      【讨论】:

      • 这是唯一的解决方案吗?我不认为这是正确的......添加空文本视图以使按钮居中?
      【解决方案5】:

      删除此行&lt;TableRow android:layout_gravity="center"&gt; 并在 heightwidth

      的表格行中给出android:fill_containt

      编辑答案: 好吧,我很抱歉最后的帖子拼写错误

      通过 XML,TableLayout 似乎是不可能的。您需要在 oncreate 中的代码中设置此 UI。 在您的 java 文件中,您可以使用下面的方法设置它们。

      playGame.setWidth(ScreenWidth()/2-var);
      noPlayGame.setWidth(ScreenWidth()/2-var);
      

      这里var可以用来设置这2个按钮之间的距离,比如10或30。 此外,您的 xml 将得到如下更改:

      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/ButtonsTable"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">
              <TableRow
                  android:layout_width="wrap_content"
                  android:layout_height="fill_parent"
                  android:gravity="center">
                  <Button
                      android:text="yes"
                      android:id="@+id/playGame"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:gravity="center"/>
                  <Button
                      android:text="no"
                      android:id="@+id/noPlayGame"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:gravity="center"/>
              </TableRow>
      </TableLayout>
      

      方法ScreenWidth()可以通过这些方法获取屏幕宽度

      private int ScreenWidth() {
              DisplayMetrics dm=new DisplayMetrics();
              getWindowManager().getDefaultDisplay().getMetrics(dm);
              String width=""+dm.widthPixels;
              return Integer.parseInt(width);
      }
      

      【讨论】:

      • android:fill_containt?我还没有在 android 的文档中找到这个“属性”。
      • 我已经编辑了问题,添加了更多细节。你的答案对我不起作用。
      猜你喜欢
      • 1970-01-01
      • 2019-11-04
      • 2010-10-30
      • 1970-01-01
      • 2014-10-01
      • 2019-01-16
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      相关资源
      最近更新 更多