Android 如何在单击按钮时使相对布局在线性布局内复制自身

Android 如何在单击按钮时使相对布局在线性布局内复制自身,android,xml,android-layout,Android,Xml,Android Layout,我在一个亲戚家里建立了一个警觉的对话。一个小部分是一个内部带有RelativeLayout的ListView。我的xml文件如下所示: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"

我在一个亲戚家里建立了一个警觉的对话。一个小部分是一个内部带有RelativeLayout的ListView。我的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>

...
...

单击按钮时,我需要在列表视图中复制内部相对布局。我该怎么做?提前谢谢

为要充气的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>

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

为要充气的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>

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

为要充气的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>

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

为要充气的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>

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

另一种方法是在父linearlayout中保留relativelayout的两个副本。将其中一个的可见性保持为“已消失”,并在希望其出现时将其标记为可见


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

另一种方法是在父linearlayout中保留relativelayout的两个副本。将其中一个的可见性保持为“已消失”,并在希望其出现时将其标记为可见


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

另一种方法是在父linearlayout中保留relativelayout的两个副本。将其中一个的可见性保持为“已消失”,并在希望其出现时将其标记为可见


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

另一种方法是在父linearlayout中保留relativelayout的两个副本。将其中一个的可见性保持为“已消失”,并在希望其出现时将其标记为可见



这种方法的好处是尽量减少代码。

将其放在另一个布局文件中并使用充气器。@njzk2如何将充气器放在这个xml文件中?您不需要。您可以从java代码中充气,显然是从单击列表中充气。将其放入另一个布局文件并使用充气器。@njzk2如何将充气器放入此xml文件中?您不需要。您可以从java代码中充气,显然是从单击列表中充气。将其放入另一个布局文件并使用充气器。@njzk2如何将充气器放入此xml文件中?您不需要。您可以从java代码中充气,显然是从单击列表中充气。将其放入另一个布局文件并使用充气器。@njzk2如何将充气器放入此xml文件中?您不需要。你从java代码中膨胀,显然是从点击列表中膨胀如果你认为它解决了你的问题,把它标记为一个答案。我试过了,它说add(view)对于LinearLayout来说不够精确。你知道这是为什么吗?@JunebugsinJuly这是一个打字错误。它应该是addView(视图)。如果你认为它解决了你的问题,请将它标记为一个答案。我试过这个,它说add(视图)在线性布局方面不够精确。你知道这是为什么吗?@JunebugsinJuly这是一个打字错误。它应该是addView(视图)。如果你认为它解决了你的问题,请将它标记为一个答案。我试过这个,它说add(视图)在线性布局方面不够精确。你知道这是为什么吗?@JunebugsinJuly这是一个打字错误。它应该是addView(视图)。如果你认为它解决了你的问题,请将它标记为一个答案。我试过这个,它说add(视图)在线性布局方面不够精确。你知道为什么吗?@JunebugsinJuly这是一个打字错误。应该是addView(view)