删除Imageview创建的所有图像

时间:2014-10-05 09:08:26

标签: android

我使用wordImage创建n个图像并分配不同的Id。这发生在按钮单击添加上。当我点击我应该从视图中移除我创建的所有图像时(Head是视图),还有另一个按钮被删除。 Plz帮助如何做到这一点。

 onclick of add button
             for (int getwordcount = 0; getwordcount <5; getwordcount++) {
                                int i=0;
                                WordImage = new ImageView(this);
                                WordImage.setId(getwordcount);
                                WordImage.setBackgroundResource(alphabetsimages[getwordcount]);
                                RelativeLayout.LayoutParams para=new RelativeLayout.LayoutParams(
                                        RelativeLayout.LayoutParams.FILL_PARENT,
                                        RelativeLayout.LayoutParams.FILL_PARENT);
                                para.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                                para.leftMargin=maragin+=54;
                                para.bottomMargin=25;
                                para.width=BtnNext.getWidth()/3;
                                para.height=BtnNext.getHeight();
                                WordImage.setLayoutParams(para);
                                WordImage.setTag(getSplitString);
                                Head.addView(WordImage);
                                } 

onclick删除按钮         只有最后一个-------------&gt;的 Head.removeView(WordImage);         从视图中删除创建图像。         如何删除使用wordimage创建的所有图像。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

WordImage 仅保存最后一个 ImageView 的链接。以前的链接丢失了这一行:

WordImage = new ImageView(this);

这就是 Head.removeView(WordImage)仅删除最后一张图像的原因。 如果你想删除所有添加的图像元素,最简单的方法是添加RelativeLayout容器,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"        >

    <RelativeLayout android:id="@+id/Head" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="add"
            android:onClick="onClickAdd"
            android:id="@+id/button" android:layout_alignParentStart="true"/>
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="remove"
            android:onClick="onClickRemove"
            android:layout_alignBaseline="@id/button"
            android:layout_alignParentRight="true"/>
</RelativeLayout>

然后您只能通过一个命令删除所有图像:

Head.removeAllViews();

在其他情况下,如果您需要删除单独的图片,则必须在周期中删除与“添加”块类似的项目:

for (int getwordcount = 0; getwordcount <alphabetsimages.length; getwordcount++) {
            Head.removeView(findViewById(getwordcount));
}

活动类将是这样的:

public class MyActivity extends Activity {

    RelativeLayout Head;
    int alphabetsimages[] = {android.R.drawable.arrow_down_float, android.R.drawable.btn_dropdown, android.R.drawable.btn_minus, android.R.drawable.ic_dialog_map};
    ImageView WordImage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


    }
    public void onClickAdd(View v){
        int margin = 0;
        for (int getwordcount = 0; getwordcount <alphabetsimages.length; getwordcount++) {
            int i=0;
            WordImage = new ImageView(this);
            WordImage.setId(getwordcount);
            WordImage.setBackgroundResource(alphabetsimages[getwordcount]);
            RelativeLayout.LayoutParams para=new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.FILL_PARENT);
            para.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            para.leftMargin=margin+=54;
            para.bottomMargin=25;
            para.width = 40;
            para.height = 40;
            WordImage.setLayoutParams(para);
            Head= (RelativeLayout)findViewById(R.id.Head);
            Head.addView(WordImage);
        }
    }

    public void onClickRemove(View v){
        //Head.removeAllViews();
        for (int getwordcount = 0; getwordcount <alphabetsimages.length; getwordcount++) {
            Head.removeView(findViewById(getwordcount));
        }
    }
}

我希望这就是你所需要的!