16

我正在开发一个 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"

4

5 回答 5

22

我希望这有帮助:

<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>
于 2011-05-13T14:12:00.743 回答
10

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

我们使用逗号分隔的列表在 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>

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

于 2012-01-30T08:45:35.917 回答
10

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

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

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

于 2012-07-15T02:42:57.657 回答
2

删除此行<TableRow android:layout_gravity="center">android:fill_containt在表格行中给出高度宽度

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

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

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

此处var可用于设置这两个按钮之间的距离,例如 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);
}
于 2010-10-06T13:47:44.513 回答
2

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

<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>
于 2010-11-12T23:01:28.393 回答