当用户点击我的按钮时如何隐藏ImageView。当点击相同的按钮时再次显示ImageView?

时间:2013-11-19 13:42:13

标签: android

当用户点击我的按钮时如何隐藏ImageView。当点击相同的按钮时再显示ImageView?

1 个答案:

答案 0 :(得分:0)

在项目中使用以下代码:

private ImageView picview;

    //*snip* loads of other code

//Show Image Button
ImageButton show_pic = new ImageButton(this);
show_pic.setBackgroundColor(Color.WHITE);
show_pic.setOnClickListener(new OnClickListener()
{

    @Override
    public void onClick(View v) {
        if(picview.getVisibility() == View.INVISIBLE)
         {
         picview.setVisibility(View.VISIBLE);
         } 
        else if (picview.getVisibility() == View.VISIBLE)
         {
         picview.setVisibility(View.INVISIBLE);
         }          
        }

});

params = new RelativeLayout.LayoutParams(40, 40);
params.topMargin = 10;
params.leftMargin = 10;
params.addRule(RelativeLayout.BELOW, button2_id);
rl.addView(show_pic, params);

//Imageview loaded from drawable
picview = new ImageView(this);
params = new RelativeLayout.LayoutParams(200, 400);
params.topMargin = 0;
params.leftMargin = 30;
params.addRule(RelativeLayout.RIGHT_OF, button2_id);
picview.setImageResource(R.drawable.my_image);
picview.setVisibility(View.INVISIBLE);
rl.addView(picview, params);

this.setContentView(rl);