0

单击 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" />
4

5 回答 5

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);
               }
     });



}
于 2013-08-26T10:33:42.680 回答
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
                }
    }
于 2013-08-26T10:26:09.823 回答
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" />
于 2013-08-26T10:31:21.177 回答
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;
            }
        });
于 2016-04-06T11:17:04.727 回答
-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;
            }
        });

这段代码对我来说工作得很好。

于 2013-08-26T10:31:39.137 回答