如何动态添加布局
How to add the layout dynamically
我想动态添加布局。实际上,在我的应用程序中,我正在制作评论部分。我在 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>
现在,由于多个用户可以添加评论。我希望一旦用户输入评论,就应该在之前的评论下方动态添加这样的新布局。
假设您在 LinearLayout
上有 id
然后在 java
文件中
Relativelayout rl = new RelativeLayout(this);
yourLinearLayout.addView(rl);
为您的布局设置 ID
someLayout.setId (12); //ID should be of integer type.
您应该制作一个 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 文件中没有布局准备代码,这使得它变得非常简单,就像我们在适配器的 getView 中所做的那样:)
如果您在步骤中遇到任何问题,请告诉我。
我想动态添加布局。实际上,在我的应用程序中,我正在制作评论部分。我在 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>
现在,由于多个用户可以添加评论。我希望一旦用户输入评论,就应该在之前的评论下方动态添加这样的新布局。
假设您在 LinearLayout
然后在 java
文件中
Relativelayout rl = new RelativeLayout(this);
yourLinearLayout.addView(rl);
为您的布局设置 ID
someLayout.setId (12); //ID should be of integer type.
您应该制作一个 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 文件中没有布局准备代码,这使得它变得非常简单,就像我们在适配器的 getView 中所做的那样:)
如果您在步骤中遇到任何问题,请告诉我。