可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm trying to adding TableRows to a TableLayout, which is working fantastically. However, I'm running into some problems with layout parameters. The spacing between the TextViews within a TableRow isn't working out like I thought it would.
My current code looks as follows.
paymentTable = (TableLayout) findViewById(R.id.paymentTable); for(i = 0; i < cursor.getCount(); i++) { TableRow row = new TableRow(this); TextView payAmount = new TextView(this); payAmount.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_AMOUNT))); payAmount.setPadding(payAmount.getPaddingLeft(), payAmount.getPaddingTop(), textView.getPaddingRight(), payAmount.getPaddingBottom()); TextView payDate = new TextView(this); payDate.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_DATE))); row.addView(payDate); row.addView(payAmount); paymentTable.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); cursor.moveToNext(); }
Afterwards, the TableLayout looks something along the lines of:
January$500 February$500 March$405
But I want it to look like:
January $500 February $500 March $405
To clarify things, I want each new TableRow and the TextViews it contains to inherit layout properties of an existing TableRow and TextView(s).
回答1:
yawus this is a good tutorial will help you set the table layout http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/
this is the xml layout
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
this is the activity code
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/8/
TableRow tableRow= new TableRow(this); ArrayList<Object> row = data.get(position); TextView idText = new TextView(this); idText.setText(row.get(0).toString()); tableRow.addView(idText); TextView textOne = new TextView(this); textOne.setText(row.get(1).toString()); tableRow.addView(textOne); TextView textTwo = new TextView(this); textTwo.setText(row.get(2).toString()); tableRow.addView(textTwo); dataTable.addView(tableRow);
回答2:
You need to add a RelativeLayout to each Table
TableRow row = new TableRow(this); row.setId(tag); // Creating a new RelativeLayout RelativeLayout relativeLayout = new RelativeLayout(this); // as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams TableRow.LayoutParams rlp = new TableRow.LayoutParams( TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT); rlp.setMargins(0, 0, 0, 0); row.addView(relativeLayout, rlp);
Then you add your TextView to the RelativeLayout. Like this
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); TextView payAmount = new TextView(this); //lp.setMargins(left, top, right, bottom) lp.setMargins(0, 0, 16, 0); lp.addRule(RelativeLayout.ALIGN_LEFT); payAmount.setLayoutParams(lp); relativeLayout.addView(payAmount); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); TextView payDate = new TextView(this); //lp.setMargins(left, top, right, bottom) lp.setMargins(0, 0, 16, 0); lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); payDate.setLayoutParams(lp); relativeLayout.addView(payDate);
This is the way I handle positioning interface components within a table row.