0

TableLayout を画面中央のボタン 5 on 5 のグリッドに配置するにはどうすればよいですか?

  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/Table5"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:padding="30dp"
            android:weightSum="5"/>
4

3 に答える 3

0
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:layout_margin="10dp"
        android:background="#ffffff"
        android:gravity="center" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b11" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b12" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b13" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b14" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b15" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b21" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b22" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b23" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b24" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b25" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b31" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="b32" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b33" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b34" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b35" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b41" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b42" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b43" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b44" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b45" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b51" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b52" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b53" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b54" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b55" />
        </TableRow>
    </TableLayout>

</RelativeLayout>

上記のコードをAndroidレイアウトXMLファイルで使用して、TableLayoutを使用して5x5ボタンマトリックスを作成します

于 2012-06-26T12:58:21.737 に答える
0

android:layout_gravity="center"に属性を追加してみてくださいTableLayout

(それがうまくいかない場合はTableLayout、 aLinearLayoutなどでラップしてください。)

于 2012-06-26T11:47:18.037 に答える
0
<GridView
        android:id="@+id/myGrid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="@dimen/grid_width"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:stretchMode="spacingWidth"
        android:verticalSpacing="8dp" />

grid_width には、寸法を設定できます (hdpi の場合は 480/5)。これがあなたの状況に当てはまるかどうかはわかりません。しかし、試してみてください

于 2012-06-26T11:49:00.760 に答える