Spinner's scrollbar style

前端 未结 3 1801
余生分开走
余生分开走 2020-12-03 15:48

I\'ve tried to change the style and the position of the scrollbar which is inside a spinner, but it does not work.

MainActivity.java:



        
相关标签:
3条回答
  • 2020-12-03 16:31

    After hours of work, I got the right solution (Thanks Khaled for guiding me to the right direction). You need a custom spinner:

    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    import org.holoeverywhere.widget.ListPopupWindow;
    import org.holoeverywhere.widget.ListView;
    import org.holoeverywhere.widget.Spinner;
    
    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class CustomSpinner extends Spinner
    {
        public CustomSpinner(Context context)
        {
            super(context);
        }
    
        public CustomSpinner(Context context, AttributeSet attrs)
        {
            super(context, attrs);
        }
    
        public CustomSpinner(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
        }
    
        public CustomSpinner(Context context, AttributeSet attrs, int defStyle, int mode)
        {
            super(context, attrs, defStyle, mode);
        }
    
        public CustomSpinner(Context context, int mode)
        {
            super(context, mode);
        }
    
        @Override
        public boolean performClick()
        {
            boolean bClicked = super.performClick();
    
            try
            {
                Field mPopupField = Spinner.class.getDeclaredField("mPopup");
                mPopupField.setAccessible(true);
                ListPopupWindow pop = (ListPopupWindow) mPopupField.get(this);
                ListView listview = pop.getListView();
    
                Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
                mScrollCacheField.setAccessible(true);
                Object mScrollCache = mScrollCacheField.get(listview);
                Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
                scrollBarField.setAccessible(true);
                Object scrollBar = scrollBarField.get(mScrollCache);
                Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
                method.setAccessible(true);
                method.invoke(scrollBar, getResources().getDrawable(R.drawable.scrollbar_style));
    
                if(VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB)
                {
                    Field mVerticalScrollbarPositionField = View.class.getDeclaredField("mVerticalScrollbarPosition");
                    mVerticalScrollbarPositionField.setAccessible(true);
                    mVerticalScrollbarPositionField.set(listview, SCROLLBAR_POSITION_LEFT);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
    
            return bClicked;
        }
    }
    

    Note that I used HoloEveryWhere library, but the same solution would work with android.widget.Spinner.

    0 讨论(0)
  • 2020-12-03 16:37

    in your app theme or activity them :

      <item name="android:dropDownListViewStyle">@style/SpinnerStyle1</item>
    

    declare SpinnerStyle1:

    <style name="SpinnerStyle1" parent="Widget.AppCompat.ListView.DropDown">
            <item name="android:divider">@color/blackText</item>
            <item name="android:dividerHeight">1px</item>
            <item name="android:scrollbars">none</item>
    </style>
    
    0 讨论(0)
  • 2020-12-03 16:54

    Spinners in Android doesn't have a Scrollbar, the scrollbar exist within the listView maintained inside the Spinner, and has no public access.

    There's a run-time way to override it in the link below ..

    Resource: https://stackoverflow.com/a/12644532/2128327

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