单击按钮后添加布局

时间:2013-12-24 15:17:55

标签: android android-layout android-view

在我的应用程序中,我必须创建一个按钮(加号)。单击该按钮后,它应该动态地将另一个视图附加到另一个视图下方。

view视图连续有两个edittext。我不知道怎么做那个。请为我推荐一些教程。我必须为此制作自定义视图吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

创建一个布局以动态生成它,布局需要包含您的电子邮件编辑文本和您的微调器。

然后将主布局创建为一个linearlayout,面向垂直,一个加号按钮。单击此按钮,您可以膨胀通用布局并将其添加到linearlayout上方,因此它会自动按下加号按钮。

答案 1 :(得分:0)

说这是你的主要布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/main_layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
   </LinearLayout>

这是你的布局,要添加(在一个单独的文件中)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/hidden_layout"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical">

    <TextView  android:id="@+id/text_view"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Hello, this is the inflated text of hidden layout"/>

    <EditText  android:id="@+id/edit_text"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Hello, this is your name"/>
</LineraLayout>

在您的活动中

LinearLayout main = (LinearLayout)findViewById(R.id.main_layout);

现在您的button.onclick代码看起来像

 View view = getLayoutInflater().inflate(R.layout.hidden_layout, main,false);
 main.addView(view);