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

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

    Sorry for my poor English. :) I think it is better to directly create the spinner in Toolbar.

    Here is a example in my fragment.

    public class Testfragment1 extends Fragment {
    
        Toolbar mToolbar;
        Spinner mSpinner;
        .....
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
            .......                  
            mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
            //you can also set the style with the constructor
            mSpinner = new Spinner(getActivity());
            String[] frags = new String[]{
                    "category1",
                    "category2",
                    "category3",
            };
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,frags);
            mSpinner.setAdapter(arrayAdapter);
            mToolbar.addView(mSpinner);
            return inflater.inflate(R.layout.fragment_testfragment1, container, false);
        }
    
        .........
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
            if (mToolbar != null && mSpinner != null) {
                mToolbar.removeView(mSpinner);
            }
        }
    }
    

    It looks fine on my android-4.1-device: android-4.1-spinner

    0 讨论(0)
  • 2020-12-04 06:47

    To pick up on this, I was having similar problems. My main problem was the text in my toolbar was smaller than the usual title dimensions and the wrong colour. Screenshot here http://s27.postimg.org/v24x1aw43/Screen_Shot_2015_01_11_at_13_36_04.png

    The dropdown menu was ok, but I will go through the customisation of that as well.

    Let me also make clear this fix is mostly based on @Daniel B's fix, however does not require the custom adapter, as far as I can tell nothing is broken, but I give no guarantees!

    1. Add a normal spinner item into the XML layout file (within the toolbar).
    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="5dp"
            android:minHeight="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:theme="@style/GalaxyZooThemeToolbarDarkOverflow"
            app:popupTheme="@style/Theme.AppCompat"
            >
    
        <Spinner
            android:id="@+id/spinner_nav"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
    
        </android.support.v7.widget.Toolbar>
    
    1. Create new layout file toolbar_spinner_item_actionbar.xml (This will be the stuff showing for the spinner in the toolbar)
    <?xml version="1.0" encoding="utf-8"?>
    
    <TextView
        android:id="@android:id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="20dp"
        android:fontFamily="sans-serif"
        android:paddingLeft="@dimen/abc_action_bar_default_padding_material"
        android:paddingRight="4dp"
        android:textColor="@color/colorDark"
        android:textSize="@dimen/abc_text_size_title_material_toolbar"
        xmlns:android="http://schemas.android.com/apk/res/android"/>
    
        <!-- android:drawableRight="@drawable/spinner_triangle" -->
    
    1. The adapter for your spinner remains pretty much the same, however switch the layout from the standard android.R.layout.simple_spinner_dropdown_item to R.layout.toolbar_spinner_item_actionbar. This will apply your custom look for the toolbar text.

    In this example I have set the adapter.setDropDownViewResource to android.R.layout.simple_spinner_dropdown_item, this applies the standard theme defaults for the drop down list, which I am happy with.

    ArrayAdapter<String> set1Adapter = new ArrayAdapter<String>(RoutineDetailsActivity.this, R.layout.toolbar_spinner_item_actionbar, set1Actual);
            set1Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            mWeekSpinner.setAdapter(set1Adapter);
    

    That's basically it, result here [can't attach image or add another link as my rep is too low! Will add in comment] . You could stop here, however you may want to change the colour of the dropdown arrow.

    Technically it is the correct tinting for my app, however, as my primary colour is already the colour for the toolbar, it would make sense to customise the arrow.

    Setup custom arrow drawable

    1. Add this line drawable line 'android:drawableRight="@drawable/spinner_triangle' into the toolbar_spinner_item_actionbar.xml made earlier. Now this could be any image, for now you could use Daniel B's white arrow resource here https://raw.githubusercontent.com/google/iosched/master/android/src/main/res/drawable-xxhdpi/spinner_triangle.png.

    Running this will result in two arrows, the white arrow and the theme default. To solve this add the style below. Again this is pulled from Daniel B's code and could probably be abridged, but for now it works....

        <style name="Widget.MyApp.HeaderBar.Spinner" parent="Widget.AppCompat.Light.Spinner.DropDown.ActionBar">
            <item name="android:background">?android:selectableItemBackground</item>
            <item name="android:dropDownSelector">?android:selectableItemBackground</item>
            <item name="android:divider">@null</item>
            <item name="android:overlapAnchor">true</item>
        </style>
    
    1. Apply the created style to the spinner...
    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="5dp"
            android:minHeight="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:theme="@style/GalaxyZooThemeToolbarDarkOverflow"
            app:popupTheme="@style/Theme.AppCompat"
            >
    
        <Spinner
            android:id="@+id/spinner_nav"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/Widget.MyApp.HeaderBar.Spinner"/>
    
    
        </android.support.v7.widget.Toolbar>
    

    The result will be something like this [again can't attach or link, will add to comment]. The padding can be set from the file setup earlier, in my case I would need to change the arrow to match the icons.

    Hope that makes some sort of sense.

    0 讨论(0)
  • 2020-12-04 06:49

    I solved it by creating new values for version 21 and 23 and adding new attribute in the spinner style android:dropDownVerticalOffset and delete it from the default style file. (my case is not related to toolbar) it's for normal spinner.

    Add this style in folders 23 and 21

    <style name="spinner_style">
        <item name="android:background">@drawable/background_spinner</item>
        <item name="android:dropDownVerticalOffset">30dip</item>
    </style>
    

    It's working perfectly on all versions. Hope this works with you!

    0 讨论(0)
提交回复
热议问题