内动态地添加我要创建具有布局如图所示的附加图像在活动的TableLayout。我在使图像看起来很接近时遇到问题。 在之前的活动中,用户输入他的地址并保存。保存时,必须创建一个包含TextView和ImageButton的新行。 TextView显示该人员的姓名,ImageButton可用于删除该人员。每次输入并保存新人的地址时,都必须创建一个新行,因此添加按钮会一直向下移动以创建一行。我添加了当前布局外观的截图。 我是Android应用程序编程的初学者,需要一些帮助。请帮助我。设计必须被一个RelativeLayout的
这里是我的代码部分:
if (requestCode == REC_INFO && resultCode == RESULT_OK && data != null) {
RecipientArray = (ArrayList<Person>) data.getSerializableExtra("RecArray");
TableLayout tbl = new TableLayout(this);
TextView[] tv = new TextView[RecipientArray.size()];
ImageButton delete_btns[] = new ImageButton[RecipientArray.size()];
TableRow tr[] = new TableRow[RecipientArray.size()];
for (int i = 0; i < RecipientArray.size(); i++) {
tv[i] = new TextView(this);
tv[i].setBackgroundResource(R.drawable.fill_rece);
Person p = RecipientArray.get(i);
tv[i].setText(p.getName());
tv[i].setTextColor(Color.WHITE);
tv[i].setGravity(Gravity.CENTER);
delete_btns[i] = new ImageButton(this);
delete_btns[i].setImageResource(R.drawable.ipad_postcare_landscape_from);
d.setBounds(0, 0, delete_btns[i].getWidth(), delete_btns[i].getHeight());
tr[i] = new TableRow(this);
tr[i].addView(tv[i]);
tr[i].addView(delete_btns[i]);
tbl.addView(tr[i]);
}
recs_layout.addView(tbl);//Adding TableLayout to parent RelativeLayout
}
下面是XML配置文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@id/top_bar_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/top_bar"
android:contentDescription="@string/content" />
<TextView
android:id="@+id/txt_recipients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:padding="8dp"
android:text="@string/text_recipients"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<ImageButton
android:id="@id/btn_back"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/content"
android:paddingTop="6dp"
android:src="@drawable/ic_back" />
<RelativeLayout
android:id="@+id/Rlayout_recipients"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="@id/top_bar_view"
android:background="@drawable/bg" >
<ImageButton
android:id="@+id/btn_rec_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:contentDescription="@string/content"
android:src="@drawable/icon_add" />
</RelativeLayout>
请使用自定义列表视图,而不是实现代码如下... – Harshid
@Harshid的ListView比使用TableLayout更好吗? – user2688158
@Harshid你能帮我一个类似或接近这个例子吗? – user2688158