Android 桌面布局放置

Android 桌面布局放置,android,android-tablelayout,Android,Android Tablelayout,如何在屏幕中心放置由5对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:

如何在屏幕中心放置由5对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"/>

尝试将
android:layout\u gravity=“center”
属性添加到
TableLayout

(如果不起作用,请用
线性布局
或类似的东西将
表格布局
包装起来。)


在网格宽度中,可以设置尺寸(对于hdpi,为480/5)。 我不知道这是否适合你的情况。但是试试看


<?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按钮矩阵

android:weightSum=“5”请勿使用:5行5列。例如,当您可以将每个项目的空间设置为linearLayout时,使用“百分比”。很高兴我帮助了您。你介意把它记下来作为回答吗?
<?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>