我遇到了一个问题,即在一个动态生成的tablelayout中处理textview(显示为存折语句)。在这里,我使用两个tablelayout,一个作为固定标题,另一个作为可滚动。我正在使用屏幕宽度概念为每个文本视图分配一定百分比的屏幕宽度。
screenWidth = getResources().getDisplayMetrics().widthPixels;
textview进入屏幕内部的主要问题,如屏幕截图所示。
在这方面寻求你的帮助。谢谢你的帮助!!
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 Class
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));
}
答案 0 :(得分:2)
如果您想对每个 TextView 应用相同的宽度,那么我认为 layout_weight 会更合适而不是宽度。
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
0,
LayoutParams.MATCH_PARENT,
1.0f);
label_dr.setLayoutParams(param);
最后一个参数是重量。
我还建议您创建一个方法,该方法将创建TextView并返回,这样您就不需要为每个TextView重复相同的代码。
完整的示例代码一步一步:
MainActivity.java
public class MainActivity extends AppCompatActivity
{
TableLayout tableLayout;
private Button button;
String[] DATPost={"03-AUG-2014","03-AUG-2014","03-AUG-2014","03-AUG-2014"};
String[] DatVal={"31-AUG-2014","31-AUG-2014","31-AUG-2014","31-AUG-2014"};
String[] Narration={"Principle","Interest","Interest","Interest"};
String[] Credit={"0.00","0.00","0.00","0.00"};
String[] Debit={"30","22","23","152"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tableLayout=(TableLayout)findViewById(R.id.drawer_layout);
addHeaderRow();
addRows();
}
private void addHeaderRow()
{
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView tvTxnDate = getTextView("TxnDate", -1,true,R.drawable.header_background);
tr.addView(tvTxnDate);
TextView tvDatVal = getTextView("DatVal", -1,true,R.drawable.header_background);
tr.addView(tvDatVal);
TextView tvNarration = getTextView("Narration", -1,true,R.drawable.header_background);
tr.addView(tvNarration);
TextView tvDebit = getTextView("DR.", -1,true,R.drawable.header_background);
tr.addView(tvDebit);
TextView tvCredit = getTextView("CR.", -1,true,R.drawable.header_background);
tr.addView(tvCredit);
tableLayout.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
private void addRows() {
int count=200;
for(int k=0;k<DATPost.length;k++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView labelvalDate = getTextView(DATPost[k], count,false,R.drawable.cell_background);
labelvalDate.setClickable(true);
tr.addView(labelvalDate);
TextView labelnarr = getTextView(DatVal[k], count,false,R.drawable.cell_background);
tr.addView(labelnarr);
TextView labeldr = getTextView(Narration[k], count,false,R.drawable.cell_background);
tr.addView(labeldr);
TextView labelcr = getTextView(Credit[k].trim(), count,false,R.drawable.cell_background);
tr.addView(labelcr);
TextView Runnbal = getTextView(Debit[k], count,false,R.drawable.cell_background);
tr.addView(Runnbal);
tableLayout.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
}
public TextView getTextView(String value, int count,boolean boldFlag,int background)
{
TextView textview = new TextView(this);
textview.setLayoutParams(new TableRow.LayoutParams(
0, TableRow.LayoutParams.WRAP_CONTENT,1.0f));
textview.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
if(count!=-1)
textview.setId(200 + count);
textview.setText(value);
textview.setTextColor(Color.BLACK);
if(boldFlag)
textview.setTypeface(Typeface.DEFAULT_BOLD);
textview.setBackgroundResource(background);
return textview;
}
}
activity_main.xml中
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
可绘制文件夹中的cell_background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#cebcbc" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
drawable文件夹中的header_background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#e1ecbb" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
这是完整的工作示例代码,没有TextView推迟......
适用于两个TableLayout
您只需要对两个TableLayout进行细微更改。正如你在你提到的那样问题t4是你的标题TableLayout而t2是你的行TableLayout所以在我的代码里面addHeaderRow()方法
private void addHeaderRow()
{
....
....
t4.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
和addRows()方法内部
private void addHeaderRow()
{
....
....
t2.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
和布局将与您实施的相同,其他代码保持不变。
答案 1 :(得分:0)
如果你想要5个相同的标签,请删除你的填充。主要问题是填充。如果你想要填充,你还需要根据它改变你的标签宽度。
label_dr.setWidth(20 * screenWidth / 100);
用
更改此行label_dr.setWidth(15 * screenWidth / 100);
如果要填充,则需要静态检查此宽度。如果您想使用填充,则此问题出现在不同的设备上。
希望有所帮助