Android:以编程方式在表中创建两列 showthedule=新的表格布局(本); ShowTimeline.setLayoutParams(新的LayoutParams(LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容)); 对于(int i=0;i
我认为您应该在此处使用填充。它将在表行内填充文本视图。我已尝试使用您的代码进行填充,并添加了一些测试行:Android:以编程方式在表中创建两列 showthedule=新的表格布局(本); ShowTimeline.setLayoutParams(新的LayoutParams(LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容)); 对于(int i=0;i,android,android-layout,Android,Android Layout,我认为您应该在此处使用填充。它将在表行内填充文本视图。我已尝试使用您的代码进行填充,并添加了一些测试行: showtimeTable = new TableLayout(this); showtimeTable.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); for(int i=0;i<object.dateList.size
showtimeTable = new TableLayout(this);
showtimeTable.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for(int i=0;i<object.dateList.size();i++){
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
/* Create testview to be the row-content. */
mytext = new TextView(this);
mytext.setText(object.dateList.get(i).getTextDate());
mytext.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
mytext.setTextAppearance(this, R.style.detaileventDate);
tr.addView(mytext);
time = new TextView(this);
time.setText(object.dateList.get(i).getTime());
time.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
time.setTextAppearance(this, R.style.detaileventTime);
tr.addView(time);
/* Add row to TableLayout. */
showtimeTable.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
layout.addView(showtimeTable);
它工作得很好。此外,您可以使用“重力”来实现您的目的。请参见此处的示例:表格布局没有列的概念,但您可以通过定义这些视图的不同宽度,在表格行中添加多个视图 下面的代码将根据指定的colSpan值指定列的大小
mytext.setPadding(20, 3, 20, 3);
time.setPadding(20, 3, 20, 3);
您可以为表格行设置布局权重参数。这将相应地排列子项。 mytext.setLayoutParams(新的TableRow.LayoutParams(TableRow.LayoutParams.FILL_父级,TableRow.LayoutParams.WRAP_内容,1f)) 最后一个参数是布局权重
int COL_COUNT=2;
int mScreenWidthInDp = getScreenWidthInDip(activityContext);
private View getColumnItem(String colText, int colSpan) {
View view = mActivity.getLayoutInflater().inflate(R.layout.table_column_view, null);
TextView textView = (TextView) view.findViewById(R.id.tablet_title);
textView.setText(colText);
int columnWidth = (mScreenWidthInDp / COL_COUNT) * span;
TableRow.LayoutParams params = new TableRow.LayoutParams(columnWidth, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
params.span = colSpan;
view.setLayoutParams(params);
return view;
}
public static int getScreenWidthInDip(Activity context) {
if (mScreenWidthInDp == 0) {
WindowManager wm = context.getWindowManager();
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int screenWidth_in_pixel = dm.widthPixels;
float screenWidth_in_dip = screenWidth_in_pixel / dm.density;
mScreenWidthInDp = (int) screenWidth_in_dip;
}
return mScreenWidthInDp;
}