【问题标题】:TableLayout placingTableLayout放置
【发布时间】:2012-06-26 20:58:21
【问题描述】:

如何在 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:padding="30dp"
            android:weightSum="5"/>

【问题讨论】:

    标签: android android-tablelayout


    【解决方案1】:
    <?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 按钮矩阵

    【讨论】:

    • 很高兴我帮助了你。您介意将其标记为答案吗?
    【解决方案2】:
    <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)。 我不知道这是否适合你的情况。不过试试看

    【讨论】:

    • android:weightSum="5" 不要meen:5行5列。例如,当您可以将每个项目的空间设置为线性布局时,就像“百分比”一样使用。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      • 2023-03-19
      • 2011-12-29
      • 2014-10-15
      • 2014-04-16
      • 2014-02-03
      • 2012-07-01
      相关资源
      最近更新 更多