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

后端 未结 15 751
别那么骄傲
别那么骄傲 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:35

    A simple way that isn't perfect, but uniform enough for both 4.x and 5.0

    img

    I removed the from the layout files and added it programmatically - that allowed for the white triangle to show up properly.

    I also created a dropdown item layout using the appcompat required color.

    layout/spinner_dropdown_item.xml, note the android:background="@color/primaryColor"

    
    
    

    And in the activity:

        SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.your_array, R.layout.spinner_dropdown_item);
        Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
        navigationSpinner.setAdapter(spinnerAdapter);
        toolbar.addView(navigationSpinner, 0);
    

    It's not perfect and the items don't highlight when you click on them, but it's good enough while we wait for the future appcompat libraries to fix these problems (here's hoping anyway).

提交回复
热议问题