设计其具有被一个RelativeLayout的内动态地添加所述TableLayout [英] Designing the TableLayout which has to be added dynamically within a RelativeLayout

查看:205
本文介绍了设计其具有被一个RelativeLayout的内动态地添加所述TableLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建具有作为附加的图像中所示的布局的活性。我有使它看起来接近如图所示,图像中的问题。
在previous活动用户进入his.her地址并保存它。当保存了一个新的行,其中包含一个TextView和的ImageButton必须创建。 TextView的显示的人的名字和ImageButton的可以用来删除该人。每一个新的人的地址被输入并保存时间,必须创建一个新的行,因此,添加按钮保持向下移动以创建一行。我加入的布局看起来如何目前的截图。我在Android应用程序编程初学者,需要一些帮助。请,请帮助我。

I have to create an activity which has the layout as shown in the attached image. I am having problems with making it look close to as shown in the image. In the previous activity a user enters his.her address and saves it. When saved a new row which contains a TextView and a ImageButton has to be created. The TextView shows name of the person and the ImageButton can be used to delete that person. Every time a new person's address is entered and saved, a new row must be created and therefore the add button keeps moving down to create a row. I have added the screenshot of how the layout looks currently. I am a beginner in android app programming and need some assistance. Kindly, please help me.

下面是我的code的一部分:

Here is part of my code:

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布局文件:

Here is the XML layout file:

<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>

推荐答案

我觉得你的的TextView 正在该表行中的所有空间,尝试,由于设置布局PARAMS好

I think your textview is taking all the space in the table row try setting layout params for that as well

有关例如:

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)

下面的第三个参数浮动类型的重量,因为它设置为 1F 的观点既您图像视图和TextView中都将占据的空间等于你可以改变它,并将其设置你所需要的。

Here the third argument of float type is weight of the view as it is set to 1f for both your image view and textview both will occupy equal space you can change it and set it what you need.

在使用重设置宽度0dp为的TextView和ImageView的

While using weight set width to 0dp for both textview and imageview

修改

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

这篇关于设计其具有被一个RelativeLayout的内动态地添加所述TableLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆