Spinner的滚动条样式

时间:2013-11-06 12:35:49

标签: java android android-layout android-spinner android-custom-view

我试图改变旋转器内部滚动条的样式和位置,但它不起作用。

MainActivity.java:

package com.example.testapp;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Spinner
        android:entries="@array/items"
        android:verticalScrollbarPosition="left"
        android:scrollbarThumbVertical="@drawable/scrollbar_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

抽拉/ scrollbar_style.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#FF196C96" />
    <corners android:radius="5dp" />
    <size android:width="2dp" />

</shape>

值/ strings.xml中:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">TestApp</string>
    <string name="action_settings">Settings</string>

    <string-array name="items">
        <item>Item 1</item>
        <item>Item 2</item>
        <item>Item 3</item>
        <item>Item 4</item>
        <item>Item 5</item>
        <item>Item 6</item>
        <item>Item 7</item>
        <item>Item 8</item>
        <item>Item 9</item>
        <item>Item 10</item>
    </string-array>

</resources>

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

截图:

enter image description here

请注意android:verticalScrollbarPosition="left"android:scrollbarThumbVertical="@drawable/scrollbar_style"如果我将其与ListView一起使用,则可以正常工作:

enter image description here

这是一个错误吗?还是有另一种方法来操纵滚动条样式和位置Spinner

4 个答案:

答案 0 :(得分:9)

经过几个小时的工作,我得到了正确的解决方案(感谢Khaled指导我走向正确的方向)。您需要一个自定义微调器:

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;
    }
}

请注意,我使用的是HoloEveryWhere库,但相同的解决方案适用于android.widget.Spinner

答案 1 :(得分:3)

Android中的Spinners没有Scrollbar,滚动条存在于Spinner内部维护的listView中,并且没有公共访问权限。

在下面的链接中有一种运行时覆盖它的方式..

资源:https://stackoverflow.com/a/12644532/2128327

答案 2 :(得分:1)

在您的应用主题或活动中:

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

声明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>

答案 3 :(得分:0)

您可以在styles.xml中创建以下样式

<style name="SpinnerScrollBar" parent="Widget.AppCompat.ListView.DropDown">
    <item name="android:scrollbarThumbVertical">@color/scrollbar_color</item>
</style>

然后将其设置为微调器主题

<Spinner
    ...
    android:theme="@style/SpinnerScrollBar"/>

如果您想要一些渐变颜色,也可以使用drawable而不是颜色