使用自定义微调器时,微调器向上打开

时间:2016-11-29 09:15:53

标签: android android-spinner

使用默认微调器时,它始终向下打开,但在使用自定义微调器时,它会向上打开。我希望旋转器始终向下打开,无论其位置如何。

这是代码

xml代码:

 <com.pro.widget.CustomSpinnerWithScrollbar
                    android:id="@+id/sp_language"
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:layout_alignParentLeft="true"
                    android:background="@drawable/bg_white_thick_border"
                    android:popupBackground="@drawable/bg_white"
                    android:spinnerMode="dropdown"
                    />

自定义微调器的代码:

  public class CustomSpinnerWithScrollbar extends Spinner
{
    public CustomSpinnerWithScrollbar(Context context)
    {
        super(context);
    }

    public CustomSpinnerWithScrollbar(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public CustomSpinnerWithScrollbar(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public CustomSpinnerWithScrollbar(Context context, AttributeSet attrs, int defStyle, int mode)
    {
        super(context, attrs, defStyle, mode);
    }

    public CustomSpinnerWithScrollbar(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();
            listview.setScrollbarFadingEnabled(false);
            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_thumb = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
            method_thumb.setAccessible(true);
            method_thumb.invoke(scrollBar, getResources().getDrawable(R.drawable.scrollbar_rounded_corner));
            Method method_track = scrollBar.getClass().getDeclaredMethod("setVerticalTrackDrawable", Drawable.class);
            method_track.setAccessible(true);
            method_track.invoke(scrollBar, getResources().getDrawable(R.drawable.scrollbar_track_spinner));

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            {
                Field mVerticalScrollbarPositionField = View.class.getDeclaredField("mVerticalScrollbarPosition");
                mVerticalScrollbarPositionField.setAccessible(true);
                mVerticalScrollbarPositionField.set(listview, SCROLLBAR_POSITION_RIGHT);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return bClicked;
    }
}

0 个答案:

没有答案