首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TableLayout放置

TableLayout放置
EN

Stack Overflow用户
提问于 2012-06-26 19:44:46
回答 3查看 316关注 0票数 0

如何在屏幕中央放置一个由5个按钮组成的网格TableLayout?

代码语言:javascript
复制
  <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"/>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-06-26 20:58:22

代码语言:javascript
复制
<?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>

使用安卓布局XML文件中的上述代码,使用TableLayout创建5x5按钮矩阵

票数 0
EN

Stack Overflow用户

发布于 2012-06-26 19:47:18

尝试向TableLayout添加android:layout_gravity="center"属性

(如果这不起作用,可以用LinearLayout或类似的东西包装TableLayout。)

票数 0
EN

Stack Overflow用户

发布于 2012-06-26 19:49:01

代码语言:javascript
复制
<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)。我不知道这对你的情况是否有效。但是试一试

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11206602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档