我的Android应用程序中有一个TableLayout
。我是TableLayout's
的新手所以我不知道如何在我的.java文件的所有行中插入数据。
这是我的xml文件,其中包含TableLayout
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:id="@+id/tabla_cabecera" android:layout_width="match_parent" android:layout_height="match_parent"></TableRow>
<TableLayout android:id="@+id/tabla_cuerpo" android:layout_height="match_parent" android:layout_width="match_parent">
<TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView></TableRow>
<TableRow android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
</TableRow>
<TableRow android:id="@+id/tableRow4" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
</TableRow>
</TableLayout>
</LinearLayout>
我知道如何夸大布局以及如何在简单的TextView
中插入数据:
View v = inflater.inflate(R.layout.subject_report_layout, container,false);
TextView tv = (TextView ) v.findViewById(R.id.tv);
TableRow tr = (TableRow) v.findViewById(R.id.tableRow2);
tv.setText("Text");
如何对表格的所有行和列执行相同的操作?
答案 0 :(得分:2)
你必须在XML文件中定义TableLayout,如下所示:
<TableLayout
android:id="@+id/tableview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_listview"
android:cacheColorHint="#00000000"
android:divider="@android:color/transparent" >
</TableLayout>
在Java文件中,您可以在下面进行编码:
tableview = (TableLayout) findViewById(R.id.tableview);
tableview.setPadding(15, 3, 15, 3);
for (int i = 0; i <10; i++) {
TableRow row = new TableRow(this);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
row.setPadding(15, 3, 15, 3);
row.setBackgroundColor(Color.parseColor("#E4CC91"));
TextView Header = new TextView(this);
Header.setGravity(Gravity.CENTER);
Header.setText("YOUR_TEXT_HEADER");
Header.setTextSize(25.0f);
Header.setPadding(15, 0, 15, 0);
Header.setTextColor(Color.parseColor("#FFFFFF"));
Header.setTypeface(null, Typeface.BOLD);
row.addView(Header);
for (int j = 0; j < 5; j++) {
TextView Values = new TextView(this);
Values.setPadding(15, 0, 15, 0);
Values.setGravity(Gravity.CENTER);
Values.setTextSize(25.0f);
Values.setTextColor(Color.parseColor("#FFFFFF"));
Values.setTypeface(null, Typeface.BOLD);
Values.setText("YOUR_TEXT_RAW");
row.addView(Values);
}
tableview.addView(row);
}