How to set dropdown arrow in spinner?

前端 未结 10 811
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 14:40

I tried to set spinner with drop down arrow but i couldn\'t fix it can anyone help me with this? I have attached the source code.

my class file:

             


        
10条回答
  •  庸人自扰
    2020-11-27 15:17

    As a follow-up to another answer, I was asked how I changed the spinner icon to get something like this:

    One pretty easy way is to use a custom spinner item layout:

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter adapter = new ArrayAdapter(
            this,
            R.layout.view_spinner_item,
            ITEMS
    );
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    

    In res/layout/view_spinner_item.xml, define a TextView with android:drawableRight pointing to the desired icon (along with any customisations to text size, paddings and so on, if you wish):

    
    
    
    

    (For the opened state, just use android.R.layout.simple_spinner_dropdown_item or similarly create a customised layout if you want to tweak every aspect of your spinner.)

    To get the background & colours looking nice, set the Spinner's android:background and android:popupBackground as shown in that other question. And if you were wondering about the custom font in the screenshot above, you'll need a custom SpinnerAdapter.

提交回复
热议问题