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によって提案された変更を追加しました。
2番目の編集:私が以下に追加した
ソリューション:。これでボタンは中央揃えになりましたが、列全体に表示されます!!!
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

私は自分のコードを投稿しました。それが他のユーザーが自分の問題(センターコンテンツ)を解決するのに役立つことを願っています。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>

お役に立てば幸いです。私の英語でごめんなさい:/

于 2012-01-30T08:45:35.917 に答える
10

1つのボタンを1つの行の中央に配置するには:

<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。このUIは、oncreateのコードで設定する必要があります。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);
}
于 2010-10-06T13:47:44.513 に答える
2

ボタンを中央に揃えるために、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 に答える