Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
581 views
in Technique[技术] by (71.8m points)

coding style - Android: How to set spinner selector to own image/icon?

I have searching and testing to set my own image/icon in a Spinner but i get error.

I have alrady with styling set background image but then the spinner icon on the right side not shows anymore.

When i do like i find a example of like this:

<Spinner style="@style/Spinner" android:spinnerSelector="@drawable/myspinner_selector" />

And here is the myspinner_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_first="true"
   android:drawable="@drawable/arrowdown"
   />    
<item android:state_middle="true" 
   android:drawable="@drawable/arrowdown"
   />       
<item android:state_last="true"
   android:drawable="@drawable/arrowdown"
   />   
<item android:state_single="true"
   android:drawable="@drawable/arrowdown"
   />

But it is in the first XMLfor spinner I get this error:

No resource identifier found for attribute 'spinnerSelector' in package 'android' `main.xml`

All example i can find on internet using the styling of spinner selector like this but i can not get it to work and can not find spinnerSelector in the reference guide on developer.android.com either.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I didn't get the answer for changing the spinner image but you can do it..

  1. Set the spinner height and width to 0dp

  2. Create a button in the onClick() listener and call click action of the spinner like this

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            spin.performClick();
        }
    });
    
  3. Change the button text to match the spinner text.

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        button.setText(items[position]);
    }
    
  4. Now you can change the image of the button easily!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...