我创建了一个自定义textview,并希望将其动态添加到表格布局中:
自定义textview代码,名为custom_table_cell:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/style_table_cell"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="Cell" >
</TextView>
我的活动代码:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_flight" >
<ScrollView android:layout_height="fill_parent"
android:layout_width="fill_parent" >
<TableLayout
android:id="@+id/tblInvoiceData"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
</ScrollView>
</TableLayout>
以及我将代码行添加到表格布局的代码:
public TextView TableGetCell(Context context, String Text, int ColumnId) {
final TextView cell = (TextView) ((Activity) context).getLayoutInflater().inflate(R.layout.custom_table_cell,
null);
cell.setText(Text);
cell.setTypeface(Graphic.GetFontFaceFarsi(context));
cell.setLayoutParams(new TableRow.LayoutParams(ColumnId));
return cell;
}
public void CreateRows() {
for (final Customer d : Customers) {
TableRow tr = new TableRow(this);
tr.addView(TableGetCell(this, String.valueOf(d.Name), 0));
tr.addView(TableGetCell(this, String.valueOf(d.Family), 0));
tr.addView(TableGetCell(this, String.valueOf(d.Age), 0));
tblInvoiceData.addView(tr);
}
}
和我的问题:为什么文本视图没有包裹?
答案 0 :(得分:1)
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView textView = new TextView(this);
textView.setLayoutParams(new TableRow.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT, 1.0f));
textView.setText("yourValue");
row.addView(textView);
tablelayout.addView(row);