单击ImageView更改Android布局的背景

时间:2013-08-26 10:17:36

标签: android android-layout android-background

点击ImageView我想改变布局的背景。我怎么能这样做我已经创建了选择器

<?xml version="1.0" encoding="utf-8"?>

    <selector 
        xmlns:android="http://schemas.android.com/apk/res/android">
            <item 
                    android:drawable="@drawable/blue_bar" 
                    android:state_pressed="true"/> 
            <item 
                    android:drawable="@drawable/gray_bg"/>
    </selector>

XML

 <ImageView
            android:id="@+id/bookButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:onClick="reviewItineary"
            android:src="@drawable/book" />

5 个答案:

答案 0 :(得分:2)

 mylayout = (RelativeLayout) findViewById(R.layout.main);//Your layout instance

imageview=(ImageView)findViewById(R.id.bookButton);
  imageview.setOnClickListener(new OnClickListener()
     {

   @Override
     public void onClick(View v) {
     // TODO Auto-generated method stub
                   myImgView.setImageResource(R.drawable. blue_bar);
                     mylayout.setImageResource(R.drawable. blue_bar);
               }
     });



}

答案 1 :(得分:0)

    lLayout = (RelativeLayout) findViewById(R.layout.main);//Your layout instance

    bookButton=(ImageView)findViewById(R.id.bookButton);
bookButton.setOnClickListener(this);


    public void onClick(View v) {

            if(v==bookButton){

    lLayout.setBackgroundColor(Color.parseColor("#000000"));//to change the color
    lLayout.setBackgroundDrawable(drwableItem);//drawable object
    lLayout.setBackgroundResource(R.id.bckResource);//resource
                }
    }

答案 2 :(得分:0)

像这样创建选择

<强> image_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/blue_bar" android:state_focused="true"/>
    <item android:drawable="@drawable/blue_bar" android:state_pressed="true"/>
    <item android:drawable="@drawable/blue_bar" android:state_selected="true"/>
    <item android:drawable="@drawable/gray_bg" />

<ImageView
            android:id="@+id/bookButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:onClick="reviewItineary"
            android:background="@drawable/image_selector.xml"
            android:src="@drawable/book" />

答案 3 :(得分:0)

您可以更改imageview的背景,而无需像imageview本身那样设置任何滤镜:

    button = (ImageView) findViewById(R.id.back);
    button.setOnTouchListener(new View.OnTouchListener() {
    private Rect rect;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
           button.setBackgroundColor(Color.WHITE);
            rect = new Rect(v.getLeft(), v.getTop(), v.getRight(),  v.getBottom());
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        button.setBackgroundColor(Color.argb(0, 0, 0, 0));
    }
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
         if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
                        backButton.setBackgroundColor(Color.argb(0, 0, 0, 0));
                    }
                }
                return false;
            }
        });

答案 4 :(得分:-1)

以编程方式,您可以执行以下操作:

bookButton=(ImageView)findViewById(R.id.bookButton);

    public void setActivityBackgroundColor(int color) {
        View view = this.getWindow().getDecorView();
        view.setBackgroundColor(color);
    }

        bookButton.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                setActivityBackgroundColor(Color.GRAY);
                return false;
            }
        });

此代码对我来说非常合适。