2012-02-17 70 views
1

我的应用程序中的一个活动(我的第一次尝试)生成多个表行,每行表中包含一对EditText和一个Button。但是,该按钮始终显示为未对齐,导致this problem - 删除按钮略微向下移动。Android:在TableRow中排列视图以编程方式

的XML布局的相关部分如下:

<ScrollView 
    android:id="@+id/countEditList" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TableLayout 
    android:id="@+id/countEditLayout" 
    android:stretchColumns="*" 
    android:gravity="center" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <!-- insert the new rows here as they are created --> 
    </TableLayout> 
</ScrollView> 

从一个数据库游标创建每一行代码执行以下操作:

TableRow row = new TableRow(this); 

EditText title = new EditText(this); 
title.setHorizontallyScrolling(true); 
title.setGravity(Gravity.CENTER); 
title.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

EditText counting = new EditText(this); 
counting.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
counting.setGravity(Gravity.CENTER); 

Button deleteCount = new Button(this); 
deleteCount.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
deleteCount.setText(R.string.deleteCount); 
deleteCount.setOnClickListener(this); 
deleteCount.setGravity(Gravity.CENTER); 

row.addView(title); 
row.addView(counting); 
row.addView(deleteCount); 
layout.addView(row,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // Earlier: layout = (ViewGroup) findViewById(R.id.countEditLayout); 

我怀疑,我应该以某种方式是设置EditTexts和Button的layout_gravity,但我无法解决如何做到这一点 - 任何人都可以提供任何建议吗?

回答

1

你可以使用一些填充调整编辑文本定位:

title.setPadding(3, 3, 3, 3); 
counting.setPadding(3, 3, 3, 3); 
+0

感谢您的答复。不幸的是,这似乎并没有改变行内deleteCount按钮的相对对齐。我是否应该为此使用其他某种布局,而不是表格? – knirirr 2012-02-17 14:17:44

+0

终于明白了! row.setGravity(Gravity.CENTER);看起来像它的伎俩。 – knirirr 2012-02-17 14:26:12