【发布时间】:2017-03-07 13:42:53
【问题描述】:
我在处理动态生成的表格布局中的文本视图时遇到了问题(显示为存折语句)。在这里,我使用了两个表格布局,一个作为固定标题,另一个作为可滚动的。我正在使用屏幕宽度概念为每个文本视图分配一定百分比的屏幕宽度。
screenWidth = getResources().getDisplayMetrics().widthPixels;
文本视图进入屏幕的主要问题,如屏幕截图所示。
在这方面寻求您的帮助。谢谢你的帮助!!
XML 布局
<ScrollView
android:id="@+id/ScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/spinner1"
android:layout_marginTop="45dp">
<TableLayout
android:id="@+id/tbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text='ewe'
android:layout_below="@id/ScrollView1" >
</TableLayout>
</ScrollView>
<TableLayout
android:id="@+id/tablelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/ScrollView1"
android:layout_marginTop="-20dp"
android:gravity="left">
</TableLayout>
Java 类
TableLayout t2 = (TableLayout) findViewById(R.id.tbl);
// t4 header fixed
TableLayout t4 = (TableLayout) findViewById(R.id.tablelayout);
t4.removeAllViews();
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.WHITE);
android.widget.TableRow.LayoutParams trlp = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
tr_head.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TextView label_dateval = new TextView(this);
label_dateval.setId(20);
label_dateval.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
label_dateval.setText("TxnDate");
label_dateval.setClickable(true);
label_dateval.setWidth(20* screenWidth / 100);
label_dateval.setTypeface(Typeface.DEFAULT_BOLD);
label_dateval.setPadding(15, 0, 5, 0);
tr_head.addView(label_dateval);// add the column to the table row
// here
TextView label_narr = new TextView(this);
label_narr.setId(21);// define id that must be unique
label_narr.setText("DatVal"); // set the text for the header
label_narr.setWidth(20 * screenWidth / 100);
//label_narr.setLayoutParams(trlp);
label_narr.setPadding(5, 0, 5, 0);
label_narr.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
label_narr.setTypeface(Typeface.DEFAULT_BOLD);
tr_head.addView(label_narr);
TextView label_dr = new TextView(this);
label_dr.setId(22);// define id that must be unique
label_dr.setText("Narration");
label_dr.setWidth(20 * screenWidth / 100);
//label_dr.setLayoutParams(trlp);
label_dr.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
label_dr.setTypeface(Typeface.DEFAULT_BOLD);// set the color
label_dr.setPadding(5, 0, 5, 0); // set the padding (if required)
tr_head.addView(label_dr);
TextView label_cr = new TextView(this);
label_cr.setId(22);// define id that must be unique
label_cr.setText("DR."); // set the text for the header
label_cr.setWidth(20 * screenWidth / 100);
label_cr.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
label_cr.setTypeface(Typeface.DEFAULT_BOLD);// set the color
label_cr.setPadding(5, 0, 5, 0);
tr_head.addView(label_cr);
TextView label_runbal = new TextView(this);
label_runbal.setId(22);// define id that must be unique
label_runbal.setText("CR."); // set the text for the header
label_runbal.setWidth(20 * screenWidth / 100);
//label_runbal.setLayoutParams(trlp);
label_runbal.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
label_runbal.setTypeface(Typeface.DEFAULT_BOLD);// set the color
label_runbal.setPadding(5, 0, 5, 0);
tr_head.addView(label_runbal);
t4.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
int rowCount = DATPost.length;
Log.d("Fill Table", "rowCount = " + rowCount);
for (int k = 0; k < rowCount; k++) {
TableRow tr = new TableRow(this);
tr.setId(100 + count);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
// Create two columns to add as table data
// Create a TextView to add date
TextView labelvalDate = new TextView(this);
labelvalDate.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
labelvalDate.setId(200 + count);
labelvalDate.setWidth(20 * screenWidth / 100);
labelvalDate.setClickable(true);
labelvalDate.setText(DATPost[k]);
labelvalDate.setPadding(15, 0, 5, 0);
labelvalDate.setTextColor(Color.BLACK);
tr.addView(labelvalDate);
TextView labelnarr = new TextView(this);
labelnarr.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
labelnarr.setId(200 + count);
labelnarr.setText(DatVal[k]);
labelnarr.setWidth(20 * screenWidth / 100);
labelnarr.setPadding(5, 0, 5, 0);
labelnarr.setTextColor(Color.BLACK);
tr.addView(labelnarr);
TextView labeldr = new TextView(this);
labeldr.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
labeldr.setId(200 + count);
labeldr.setWidth(20 * screenWidth / 100);
labeldr.setText(Narration[k]);
labeldr.setPadding(5, 0, 5, 0);
labeldr.setTextColor(Color.BLACK);
tr.addView(labeldr);
TextView labelcr = new TextView(this);
labelcr.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
labelcr.setId(200 + count);
labelcr.setText(Credit[k].trim());
labelcr.setPadding(5, 0, 5, 0);
labelcr.setWidth(20 * screenWidth / 100);
labelcr.setTextColor(Color.BLACK);
tr.addView(labelcr);
TextView Runnbal = new TextView(this);
Runnbal.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
Runnbal.setId(200 + count);
Runnbal.setText(Debit[k]);
Runnbal.setWidth(20 * screenWidth / 100);
Runnbal.setPadding(5, 0, 5, 0);
Runnbal.setTextColor(Color.BLACK);
Runnbal.setGravity(Gravity.CENTER_HORIZONTAL);
tr.addView(Runnbal);
t2.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
【问题讨论】:
-
label_dr.setWidth(20 * screenWidth / 100);用 label_dr.setWidth(15 * screenWidth / 100); 改变这一行;
-
您的 label_dr 占用了太多空间。减少它然后尝试
-
主要问题是您要划分 5 个标签,这很好。但你也给填充。这就是为什么你的最后一个标签要出去了。希望你明白这一点
标签: android textview tablelayout