Android:以编程方式排列表行中的视图

时间:2012-02-17 12:34:39

标签: android android-layout

我的应用程序中的一个活动(我的第一次尝试)生成了几个表行,每个行包含一些EditTexts和一个Button。但是,按钮始终显示为未对齐,导致this problem - 删除按钮略微向下移动。

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 个答案:

答案 0 :(得分:1)

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

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