Android:如何将微调选择器设置为自己的图像/图标? [英] Android: How to set spinner selector to own image/icon?

查看:195
本文介绍了Android:如何将微调选择器设置为自己的图像/图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有搜索和测试在微调框中设置我自己的图像/图标,但我收到错误。



我已经改变了样式设置背景图像,



当我喜欢我找到一个像这样的例子:

 < Spinner style =@ style / Spinnerandroid:spinnerSelector =@ drawable / myspinner_selector/> 

这里是myspinner_selector.xml:

 <?xml version =1.0encoding =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
/>



XML 为spinner我得到这个错误:

 找不到资源标识符属性'spinnerSelector'in package'android'`main.xml` 

使用这样的微调选择器的样式,但我不能得到它的工作,并且不能在 spinnerSelector 。 android。rel =nofollow> developer.android.com

解决方案


  1. 将微调框的高度和宽度设置为0dp

    p>

  2. 在onClick()监听器中创建一个按钮,并像下面这样调用微调器的点击操作:

      button.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
    spin.performClick
    }
    });


  3. 更改按钮文字以符合微调文字。



    public void onItemSelected(AdapterView<?> parent,View v,int position,long id){
    button.setText(items [position] ;
    }


  4. 现在您可以轻松更改按钮的图像! p>



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.

解决方案

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!

这篇关于Android:如何将微调选择器设置为自己的图像/图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆