2013-12-24 174 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 –

相关问题