Android: Using selector to set background color for image view

后端 未结 5 990
情歌与酒
情歌与酒 2020-12-15 22:45

I am trying to set the background color of an image view.

 

        
相关标签:
5条回答
  • 2020-12-15 23:16

    Best way to implement the selector is by using the xml instead of using programatic way as its more easy to implemnt with xml.

        <?xml version="1.0" encoding="utf-8"?>    
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
           <item android:state_selected="true">
        <color android:color="@color/Violet" />
    </item>
    <item android:state_pressed="true">
        <color android:color="@color/red" />
    </item>
    <item>
        <color android:color="@color/white" />
    </item>
    
        </selector>
    

    For more information i implemented using this link http://www.blazin.in/2016/03/how-to-use-selectors-for-botton.html

    0 讨论(0)
  • 2020-12-15 23:21
     <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:drawable="@color/black_mediam" />
        <item android:state_pressed="true" android:drawable="@color/black_mediam" />
        <item android:drawable="@color/menu_bg" />
    </selector>
    

    it is working for ImageView and Button

          <Button
                android:layout_weight="1"
                android:background="@drawable/image_selecter"
                android:id="@+id/btn_delete_gif"
                android:textAllCaps="false"
                android:text="Yes"
                android:textSize="@dimen/_13sdp"
                android:textColor="@android:color/white"
                android:layout_width="175dp"
                android:layout_height="wrap_content" />
    
    
         <ImageView
                android:padding="@dimen/_3sdp"
                android:background="@drawable/image_selecter"
                android:layout_weight="1"
                android:layout_alignParentRight="true"
                android:id="@+id/delete_gif_giflistview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_delete_gif"
                android:layout_marginLeft="1dp" />
    
    0 讨论(0)
  • 2020-12-15 23:26

    You cannot use background tag in selector file.Instead,you can use it like:

    1. Create a color.xml in "values" folder under "res" folder:

    color.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <drawable name="color_one">#110000</drawable>
        <drawable name="color_two">#001111</drawable>
        <drawable name="color_three">#001100</drawable>    
    </resources>
    

    2. Now,Use this file in your selector file like:

     <?xml version="1.0" encoding="utf-8"?> 
     <selector xmlns:android="http://schemas.android.com/apk/res/android">     
         <item android:state_selected="true" android:drawable="@drawable/color_one" />    
         <item android:state_pressed="true" android:drawable="@drawable/color_two" />
         <item android:drawable="@drawable/color_three"/>
     </selector>
    
    0 讨论(0)
  • 2020-12-15 23:35

    Your Logcat:

    E/AndroidRuntime( 4196): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: tag requires a 'drawable' attribute or child tag defining a drawable

    LogCat is pointing out that your <item> tag should include android:drawable tag.

    You selector file should look like this:

    <?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android">     
         <item android:state_selected="true" android:drawable="@color/Violet" />    
         <item android:state_pressed="true" android:drawable="@color/red" />
         <item android:drawable="@color/white" />
    </selector>
    
    0 讨论(0)
  • 2020-12-15 23:36

    The simplest way to do what you want is adding a child node to the item instead of using its drawable attribute:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true">
            <color android:color="@color/Violet" />
        </item>
        <item android:state_pressed="true">
            <color android:color="@color/red" />
        </item>
        <item>
            <color android:color="@color/white" />
        </item>
    </selector>
    

    Here you can use direct values for color as well (e.g. #FFFF0000 instead of "@color/red") to save yourself from defining many color resources.

    0 讨论(0)
提交回复
热议问题