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):
By this 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>
The problem is that when I try to make one of the parts of this grid programmatically 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);
}
}
This code makes half the screen some_random_color_for_debugging1, with a background color of some_random_color_for_debugging.
How can I make it show both TableRow row and box?
weight
is being use with impact on performance. – user