Android Spinner - How to position dropdown arrow as close to text as possible when options have different length?

后端 未结 2 1718
借酒劲吻你
借酒劲吻你 2021-01-11 15:04

The options in my spinner has different length and currently the dropdown arrow is positioned far to the right based on the longest option, as shown in the screenshot below.

2条回答
  •  春和景丽
    2021-01-11 15:33

    You cannot control the position of the original dropdown icon, the only way is to disable the default icon and add your own one into the dropdown.

    First, disable the default dropdown icon by setting the background of the Spinner to @null:

    
    

    Then create a layout resource spinner_item_main.xml with only one TextView which we can set a drawable on its right side (you can download the arrow picture from here):

    
    

    Finally Set this layout resource when you initialize the Spinner, You can also provide a resource as the dropdown view (as what I have done):

    (I use kotlin)

    spinner_main.adapter = ArrayAdapter(this,
                R.layout.spinner_item_main, objects).apply {
            setDropDownViewResource(R.layout.spinner_dropdown_view_main)
        }
    

    Make it! View this in My APP

提交回复
热议问题