內動態地添加我要創建具有佈局如圖所示的附加圖像在活動的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