Android:以编程方式在表行中排列 View

标签 android android-layout

我的应用程序中的一个 Activity (我第一次尝试)生成几个表格行,每个表格行包含几个 EditText 和一个 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,但我不知道该怎么做 - 谁能提供任何建议?

最佳答案

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

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

关于Android:以编程方式在表行中排列 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9328277/

相关文章:

android - 在 Android 上运行时移动小部件

java - 尝试从 SQL 数据库检索数据到 listview android 时收到空指针异常

android - GCM : why my application crash in GCMRegistrar. 检查设备(这个);

android - relativelayout 中的第一个 View 展开以填充剩余空间

android - Activity 中的默认 fragment

android - 使用 set 不会影响带有数据绑定(bind)的 View

android - 为什么我的 SVG 无法在 Vector Asset Studio 中加载

整个列表左侧带有小圆圈和线条的 Android ListView ?

android - 从 anchor 滑动到折叠状态时,Umano SlidingUpPanel 卡住