How can I fix the Spinner style for Android 4.x placed on top of the Toolbar

后端 未结 15 788
别那么骄傲
别那么骄傲 2020-12-04 06:10

According to Android documentation, Material Design style is supported for Spinner widget.

So I decided to use it in my app placing it on top of the Toolbar.

<

15条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 06:39

    I've wasted hours on this issue. As far as I can tell, the above solutions all require copy/pasting large chunks of appcompat style code to reimplement basic details like touch states.

    A relatively easy way to get native-like behaviour is to inflate the view programmatically to ensure it gets the correct theme, e.g.:

    // Activity has context with 'Theme.AppCompat.Light.NoActionBar'
    spinner = new AppCompatSpinner(getActivity());
    toolbar.addView(spinner);
    

    To get the triangle to be white rather than colorControlNormal, I've applied a ColorStateList tint to the background:

    ViewCompat.setBackgroundTintList(spinner, resources.getColorStateList(R.drawable.bg_toolbar_spinner)
    

    bg_toolbar_spinner.xml

    
        
        
    
    

提交回复
热议问题