如何使相对布局在线性布局内重复单击按钮单击

时间:2014-08-05 18:26:15

标签: android xml android-layout

我在RelativeLayout中设置了一个警报对话框。一小部分是一个ListView,里面有RelativeLayout。我的xml文件如下:

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

单击该按钮时,我需要内部相对布局在列表视图中复制自身。我怎样才能做到这一点?提前谢谢!

2 个答案:

答案 0 :(得分:2)

为要扩充的 RealtiveLayout 创建单独的布局xml文件。

示例

父页面的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代码

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);
    }

使用上述模式,您可以动态添加新视图。

答案 1 :(得分:0)

另一种方法是在父线性布局中保留relativelayout的两个副本。保持其中一个人的可见度不再“消失”。并在希望它出现时将其标记为可见。

这种方法的好处是最小化代码。

相关问题