如何使一个相对布局复制自身内部线性布局在按钮单击 [英] How to make a Relative Layout Duplicate Itself Inside a Linear Layout On Button Click

查看:258
本文介绍了如何使一个相对布局复制自身内部线性布局在按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个警报对话建立一个RelativeLayout的内部。一小部分是内部RelativeLayout的一个ListView。我的xml文件如下:

I have an alert dialogue set up inside a RelativeLayout. A small section is a ListView with a RelativeLayout inside. My xml file is as follows:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:padding="10dp">

 ...

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:layout_below="@id/bar_main"
    android:id="@+id/dialoguelistview"
    android:padding="10dp">
<RelativeLayout>    
    <EditText
        android:id="@+id/Item"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"          
        android:hint="@string/Item" 
        />

    <EditText
        android:id="@+id/Quantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_alignParentLeft="true"
        android:hint="@string/Quantity" 
        android:layout_below="@id/Item"/>

    <EditText
        android:id="@+id/Cost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_toRightOf="@id/Quantity"
        android:hint="@string/Cost" 
        android:layout_below="@id/Item"/>

    <Button
        android:id="@+id/Button01"
        android:layout_width="30dip" 
        android:layout_height="30dip" 
        android:layout_alignParentRight="true"
        android:layout_marginTop="20dp"
        android:layout_marginRight="10dp"
        android:layout_below="@id/Item"
        android:background="@drawable/round_button"
        android:text="@string/another_item"
        android:textColor="#fff" 
        android:onClick = "addAnotherItem"></Button>

 </RelativeLayout>
 </ListView>

...

</RelativeLayout>
</ScrollView>

在单击该按钮,我需要内部相对布局重复自己的列表视图中。我怎样才能做到这一点?在此先感谢!

When the button is clicked, I need the inner relative layout to duplicate itself inside the list view. How can I do this? Thanks in advance!

推荐答案

创建一个单独的布局xml文件中的 RealtiveLayout 要膨胀。

Create a separate layout xml file for RealtiveLayout that you want to inflate.

示例

父页面的XML是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="em.example.InfoActivity">

<LinearLayout 
    android:id="@+id/infolayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="15dp"
    android:layout_marginTop="50dp">
  </LinearLayout>
</RelativeLayout>

儿童页

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#55934d"
android:padding="5dp"> 

<TextView 
    android:id="@+id/TextAddress"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TEXT ADDRESS:"
    android:textSize="10sp"
    android:textColor="#fff"
    />

</RelativeLayout>

这是你的activity.java code

public void addChildLayout(){
    //Inflater service
    LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //parent layout xml refrence
    LinearLayout linearLayout=(LinearLayout)findViewById(R.id.infolayout);
    //Child layout xml refrence
    View view=layoutInfralte.inflate(R.layout.infoitem, null);
    //add child to parent
    linearLayout.addView(view);
    }

使用上面的图案可以动态地添加新的意见。

Using the above pattern you can dynamically add new views.

这篇关于如何使一个相对布局复制自身内部线性布局在按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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