2013-12-24 172 views
0

內動態地添加我要創建具有佈局如圖所示的附加圖像The image describes the layout which is required.在活動的TableLayout。我在使圖像看起來很接近時遇到問題。 在之前的活動中,用戶輸入他的地址並保存。保存時,必須創建一個包含TextView和ImageButton的新行。 TextView顯示該人員的姓名,ImageButton可用於刪除該人員。每次輸入並保存新人的地址時,都必須創建一個新行,因此添加按鈕會一直向下移動以創建一行。我添加了當前佈局外觀的截圖。 Current Layout's screenshot我是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> 

+1

請使用自定義列表視圖,而不是實現代碼如下... – Harshid

+0

@Harshid的ListView比使用TableLayout更好嗎? – user2688158

+0

@Harshid你能幫我一個類似或接近這個例子嗎? – user2688158

回答

0

我覺得你textview走的是錶行中的所有空間嘗試設置佈局PARAMS爲該以及

對於前:

tv[i].setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 3f)); 

同樣地,對於圖像按鈕

delete_btns[i].setLayoutParams(new TableRow.LayoutParams(
        0, LayoutParams.WRAP_CONTENT,1f)); 

而且到錶行

tr[i].setWeightSum(4f) 

這裏float類型的第三個參數是重量,因爲它被設置爲1f爲視圖的兩個你圖像視圖和文本視圖都佔用相同的空間,您可以更改它並將其設置爲您需要的值。當使用重量設定寬度0dp兩者的TextView和ImageView的

編輯

LayoutParams lp=new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT); 
lp.weight=3f; 
tv[i].setLayoutParams(lp); 
+0

出於某種原因TableLayout.LayoutParams使得TextView的和無形的ImageButton。此外,當我把零作爲width..it使得它無形的,我必須使它LayoutParams.MATCH_PARENT/WRAP_CONTENT做出的tablelayout.layoutparams嘗試TableRow.layoutparams它可見 – user2688158

+0

代替。這將工作 –

+0

盡我的編輯ANS –

相關問題