无法添加的TableRow在另一个编程 [英] Unable to add TableRow inside another programatically

查看:185
本文介绍了无法添加的TableRow在另一个编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用TableLayout一个复杂的网格。我管理由这样的XML(有的在电网的箱子是不突出),使其:

I am trying to make a complex grid using TableLayout. I manage to make it by xml like this (some of the boxes in the grid is not highlighted):

通过这一code:

<?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>

问题是,当我试图让这个电网的组成部分之一编程它只是显示的TableRow那是TableLayout的直接孩子,不属于另一个的TableRow的儿女TableRows。我用这个Java code:

The problem is that when I try to make one of the parts of this grid programatically it is only shows the TableRow that is direct children of the TableLayout, and not the TableRows that are children of another TableRow. I use this Java code:

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);      
    } 


}

这code使得屏幕some_random_color_for_debugging1一半,some_random_color_for_debugging的背景颜色。

This code makes half the screen some_random_color_for_debugging1, with a background color of some_random_color_for_debugging.

我怎样才能使它同时显示的TableRow行和盒子?

How can I make it show both TableRow row and box?

推荐答案

我想的TableRow需要一个TableLayout作为父母才能正常工作。为了与TableLayout /你的问题的工作的TableRow你需要添加的每一行内一个新的TableLayout。也许你可以用手与你提出的结构调整,并排序它可能工作,因为TableLayout / TableRows刚煮熟LinearLayouts。

I think TableRow needs a TableLayout as a parent to properly work. To make your question work with TableLayout/TableRow you'd need to add a new TableLayout inside every row. Probably you can tweak it by hand with the structure you're proposing and it sort of might work, because TableLayout / TableRows are just cooked LinearLayouts.

不过,如果你接受了建议,我强烈建议你不要使用TableLayout的都没有。我已经使用了他们很多在我最后的应用程序做的事情类似你们,终于取代所有的人都用的FrameLayout的和坐标计算。在我的经验TableLayouts的表现太可怕了,特别是如果你把里面的TextViews。只是10×10的表格吃我的平板电脑处理器,而的FrameLayout方法只是苍蝇预期。如果你看看一个问题,我发布前一段时间(超慢tablelayout效果) ,你会看到有抱怨性能,以及一些相关的问题....

But If you accept the advice, I strongly advise you against using TableLayout's at all. I've been using a lot of them in my last app to do things similar to yours, and finally substituted all of them with FrameLayout's and coordinate calculation. The performance of TableLayouts in my experience is just terrible, specially if you put TextViews inside. A table of just 10x10 eats the processor on my tablet, while the FrameLayout approach just flies as expected. If you take a look at a question I posted some time ago (ultra-slow tablelayout performance) , you'll see there are some related questions complaining about performance as well ....

这篇关于无法添加的TableRow在另一个编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆