For example I create 4 tablerows programatically and I want each tablerow height to be 1/4 size of the screen. Each tablerow height is half the screen size. I tried different ways, but tablerow height didn’t change. I manage change tablerow width, but height doesn’t.
Here is my code :
@SuppressLint("NewApi")
public class GameActivity extends Activity {
private Context context;
public void drawTable(){
int i;
TableLayout table = new TableLayout(context);
table.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
//table.setStretchAllColumns(true);
//table.setShrinkAllColumns(true);
//table.setWeightSum(1.0F);
TableRow[] rows = new TableRow[5];
for(i = 0; i < 4; ++i){
rows[i] = new TableRow(context);
rows[i].setBackgroundResource(R.drawable.images214);
rows[i].setWeightSum((float)1.0);
rows[i].setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 10));
//rows[i].setScaleY(0.25F);
table.addView(rows[i]);
}
LinearLayout l = (LinearLayout) findViewById(R.id.linear);
l.addView(table);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
context = this;
// TableLayout table = new TableLayout(this);
// TableRow row1 = new TableRow(this);
// TableRow row2 = new TableRow(this);
// row1.addView(new EditText(this));
// row2.addView(new Button(this));
// table.addView(row1);
// table.addView(row2);
// //table.setBackground(getResources().getDrawable(R.drawable.ic_launcher));
// table.setBackgroundResource(R.drawable.ic_launcher);
// LinearLayout l = (LinearLayout) findViewById(R.id.linear);
// l.addView(table);
drawTable();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_game, menu);
return true;
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:layout_weight="1">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical|horizontal">
</LinearLayout>
</ScrollView>
</RelativeLayout>
You’re not using the proper
LayoutParams, for the tableTableLayoutyou should setLinearLayout.LayoutParams(as you add the table to aLinearLayout) and for yourTableRowstheLayoutParamsshould beTableLayout.LayoutParams(as they are children ofTableLayout). Even with the above changes things will not work because theTableLayoutis a widget that imposes constraints on its children, most notable the height will be automatically set toWRAP_CONTENTforTableRows. The only way to change this is to useweightlike this:But this will not work, because your
TableLayoutis set to fill the parent, parent which is aScrollViewwhich doesn’t imposes dimensions on its child. I don’t know what you’re trying to do with thatScrollViewthere(especially as you want the table to fill the entire screen) so I don’t know what to recommend you. Also, I don’t know your full layout but your nesting to many layouts.