我想动态添加布局。实际上,在我的应用程序中,我正在发表评论部分。我在LinearLayout中做出的注释视图以及在其中添加了ImageView(UserPics),Edittext和Post按钮的视图。
用于注释的XML如下所示:
<LinearLayout
android:id="@+id/commment_section"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:orientation="vertical"
android:layout_below="@+id/nmd_user_img"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/cmt_user_image"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_margin="5dip"
android:padding="5dip"
android:background="@color/layout_bg"/>
<EditText
android:id="@+id/cmt_user_edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/cmt_user_image"
android:layout_marginLeft="10dip"
android:padding="5dip"
android:hint="Enter your comments"/>
<ImageButton
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="5dip"
android:layout_marginRight="15dip"/>
</RelativeLayout>
</LinearLayout>
现在,由于多个用户可以添加评论。我想在用户输入评论后立即将这样的新布局动态添加到先前的评论下方。
您应该创建一个注释行的xml视图,并为它添加运行时,并将其添加到父视图中。
假设comment.xml是要动态添加的注释行的视图,因此请在下面添加它:
LayoutInflater inflator = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View commentView = inflator.inflate(R.layout.your_xml, null);
commentView.setId(commentID);
yourLinearLayout.addView(commentView);
您可以使用视图对象并调用findViewById在注释行中设置任何内容。这样,java文件中就没有布局准备代码,这使得它真的很简单,就像我们以前在adapter的getView中所做的那样:)
如果您在步骤中遇到任何困难,请告诉我。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句