android - 无法以编程方式在另一个内部添加 TableRow

标签 android tablerow android-tablelayout

我正在尝试使用 TableLayout 制作复杂的网格。我设法通过这样的 xml 来实现(网格中的某些框未突出显示):

enter image description here

通过这段代码:

<?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="fill_parent"
   android:orientation="vertical">

<FrameLayout 
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" 
   android:layout_weight="0.15">


    <TableLayout 
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">

        <TableRow
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="@android:color/holo_green_light"
             android:layout_weight="1">
             <TableRow
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="@android:color/holo_green_light"
             android:layout_weight="1">

            </TableRow>
            <TableRow
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="@android:color/holo_orange_light"
             android:layout_weight="1">

            </TableRow>

            </TableRow>
         <TableRow
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:layout_weight="1">

              <TableRow
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="@android:color/holo_orange_light"
             android:layout_weight="1">

            </TableRow>
            <TableRow
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="@android:color/holo_green_light"
             android:layout_weight="1">

            </TableRow>


        </TableRow>          
    </TableLayout>

    <TableLayout 
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
         <TableRow 
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
              android:background="@android:color/holo_green_light"
             android:layout_weight="1"/>
         </TableLayout>

     <TableLayout 
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
        <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_orange_light"
            android:layout_weight="1">

             <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_orange_light"
            android:layout_weight="1"/>                 
            <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_red_light"
            android:layout_weight="1"/>
            <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_blue_light"
            android:layout_weight="1"/>
            <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_green_light"
            android:layout_weight="1"/>

        </TableRow>

        <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_green_light"
            android:layout_weight="1">

            <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_green_light"
            android:layout_weight="1"/>
            <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_orange_light"
            android:layout_weight="1"/>                 
            <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_red_light"
            android:layout_weight="1"/>
            <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_blue_light"
            android:layout_weight="1"/>

        </TableRow>

         <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_blue_light"
            android:layout_weight="1">

            <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_blue_light"
            android:layout_weight="1"/>
             <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_green_light"
            android:layout_weight="1"/>
             <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_orange_light"
            android:layout_weight="1"/>                 
            <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_red_light"
            android:layout_weight="1"/>

        </TableRow>

        <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_red_light"
            android:layout_weight="1">

            <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_red_light"
            android:layout_weight="1"/>
             <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_blue_light"
            android:layout_weight="1"/>
              <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_green_light"
            android:layout_weight="1"/>
             <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/holo_orange_light"
            android:layout_weight="1"/>     

        </TableRow>
         </TableLayout>

    </FrameLayout>
  <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="0.85"></LinearLayout>

问题是,当我尝试以编程方式制作此网格的一部分时,它仅显示作为 TableLayout 的直接子级的 TableRow,而不显示作为另一个 TableRow 的子级的 TableRow。我使用这段 Java 代码:

TableLayout table1 = new TableLayout(this);
//Add TableLayout to a FramLayout holding multiple TableLayouts
frame.addView(table1);
table1.setBackgroundResource(R.color.some_random_color_for_debugging);


for(int num = 0; num<2; num++){
    //4x2
    TableRow row = new TableRow(this);
    TableLayout.LayoutParams par = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT, 1f);
    row.setLayoutParams(par);
    if(num==1){
        row.setBackgroundResource(R.color.some_random_color_for_debugging1);
    }
    table1.addView(row, par);       
    for(int num1 = 0; num1<2; num1++){
        //2x2
        TableRow box = new TableRow(this);
        TableRow.LayoutParams param = new TableRow.LayoutParams();
        box.setLayoutParams(param);
        if(num1==1){
            box.setBackgroundResource(R.color.some_random_color_for_debugging2);
        }
        row.addView(box, par);      
    }
}

此代码使屏幕的一半成为 some_random_color_for_debugging1,背景颜色为 some_random_color_for_debugging。

如何让它同时显示 TableRow 行和框?

最佳答案

我认为 TableRow 需要一个 TableLayout 作为父级才能正常工作。要使您的问题适用于 TableLayout/TableRow,您需要在每一行内添加一个新的 TableLayout。也许您可以根据您提出的结构手动调整它,它可能会起作用,因为 TableLayout/TableRows 只是熟制的 LinearLayouts。

但是如果您接受建议,我强烈建议您完全不要使用 TableLayout。我在上一个应用程序中使用了很多它们来做与您的类似的事情,最后用 FrameLayout 和坐标计算替换了所有这些。根据我的经验,TableLayouts 的性能非常糟糕,特别是如果您将 TextViews 放入其中。一张只有 10x10 的表格占用了我平板电脑上的处理器,而 FrameLayout 方法却按预期运行。如果您看一下我之前发布的问题 (ultra-slow tablelayout performance),您会发现有一些相关的问题也在提示性能......

关于android - 无法以编程方式在另一个内部添加 TableRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18032204/

相关文章:

Android 保留 AsyncTask 状态/进度

android - Asmack 18 连接 : google. com :5222 Exception: Could not connect to talk. google.com 远程服务器超时

java - TableLayout 内的 TableRow 导致段错误?

android - 以编程方式在表格布局中的按钮之间留出空间

jquery-validate - 使用 jQuery Validate 确保我的表中至少有一行

java - 如何修复android中的ArithmeticException错误

java - React Native Android 运行导致 "Illegal number of arguments for updateHotspot command"

java - 在同一 Activity 中从工具栏搜索 View 获取查询字符串?

javascript - 向表中添加行并更改其背景颜色

android - 如何使 TextView 中的文本在布局 XML 中不可点击?