对齐 TableRow 内的按钮

发布于 2024-09-26 00:33:49 字数 2256 浏览 0 评论 0原文

我正在开发一个 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"。现在按钮居中对齐,但它们填满了整个列!

I'm developing an Android application.

I have this:

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

I want to put playGame button in the left side of the center, and noPlayGame button in the right side of the center.

Now, both appear aligned to the left of the TableRow.

How can I do this?

Thank you.

EDIT: I've added the complete layout and the modifications suggested by Ashay.
2nd EDIT:
THE SOLUTION
I've added to TableLayout the following: android:stretchColumns="0,1". Now the buttons are center aligned but they fill their entiry column!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

柒七 2024-10-03 00:33:49

我希望这有帮助:

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

I hope this helps:

<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>
你另情深 2024-10-03 00:33:49

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

我们使用逗号分隔列表指示哪些列将在 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>

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

I posted my code, I hope that will help others users to resolve theirs problems (center content). I wanted to center two buttons in table row, here is my code, which solve this problem:

we indicate which columns will be stretched in prop android:strechColumns="a,b.." using comma-delimited list

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

I hope that it will help. Sorry for my english :/

当梦初醒 2024-10-03 00:33:49

将单个按钮放在单行上居中:

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

查看上述答案后,我从每个答案中获取了一些内容,这对我有用。

For centering a single button on a single row:

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

After reviewing the above answers, I grabbed a few things from each answer and this worked for me.

給妳壹絲溫柔 2024-10-03 00:33:49

我添加了两个虚拟文本视图以将按钮对齐在中心。

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

I added two dummy textviews to align my button in the center.

<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>
无尽的现实 2024-10-03 00:33:49

删除此行
并在表格行中给出 heightandroid: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);
}

Remove this line <TableRow android:layout_gravity="center">
And give android:fill_containt in table row for both height & width

EDITED ANSWER:
Well, I'm so sorry for the last posts spelling mistake

Through XML it seems impossible for TableLayout. You need to set this UI in code in oncreate.
In your java file you can set those by using below.

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

here var can be used to set the distance between these 2 buttons like 10 or 30.
Also, your xml will get change as follows:

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

The method ScreenWidth() can be used for getting width of the screen through these methods

private int ScreenWidth() {
        DisplayMetrics dm=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        String width=""+dm.widthPixels;
        return Integer.parseInt(width);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文