问题
I want to show data in table layout. Number of data is dynamic, so I need to implement dynamic rows in table. For this purpose I want to show 4 TextView in a single row.
Problem
Currently, I am able to show data in table and with four TextView, but I want some spaces between all four EditText, but unable to do it.
Here is my code
for(ItemRow row : this.getRows()) {
if(row != null) {
// delcare a new row
newRow = new TableRow(this);
newRow.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
newRow.setPadding(0, 2, 0, 2);
column1 = new TextView(this);
column2 = new TextView(this);
column3 = new TextView(this);
column4 = new TextView(this);
column1.setTextColor(Color.BLACK);
column2.setTextColor(Color.BLACK);
column3.setTextColor(Color.BLACK);
column4.setTextColor(Color.BLACK);
column1.setSingleLine(false);
column2.setSingleLine(false);
column3.setSingleLine(false);
column4.setSingleLine(false);
if(row.getColumnOne() != null){
column1.setText(row.getColumnOne());
}
if(row.getColumnTwo() != null) {
column2.setText(row.getColumnTwo());
}
if(row.getColumnThree() != null) {
column3.setText(row.getColumnThree());
}
if(row.getColumnFour() != null) {
column4.setText(row.getColumnFour());
}
//Now add a row
newRow.addView(column1);
newRow.addView(column2);
newRow.addView(column3);
newRow.addView(column4);
//Add row to table
table.addView(newRow, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
And here is my output :
Need
I need spaces between columns. How can I do it?
回答1:
Set padding left 5dp for every textveiw widget like android:paddingLeft="5dp"
回答2:
You have to add paddind for columns too as:
column1.setPadding(5, 5, 5, 5);
column2.setPadding(5, 5, 5, 5);
column3.setPadding(5, 5, 5, 5);
column4.setPadding(5, 5, 5, 5);
Edits: Then you can set layoutparams as follows:
column1.setLayoutParams(new LinearLayout.LayoutParams(60,90));
来源:https://stackoverflow.com/questions/6660849/problem-to-display-dynamic-rows-in-table-layout-android