更改ActionBar Spinner分隔线颜色

时间:2014-05-07 17:31:51

标签: android drop-down-menu android-actionbar android-spinner

我的ActionBar中有以下Spinner:

enter image description here

“我的购物清单”项目已停用。正如您所看到的,尽管我的主题如下所示,但是分隔符是黑色的:

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

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

    <item name="android:actionBarWidgetTheme">@style/Theme.Apptheme.Widget</item>

  </style>

</resources>

风格:

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

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Apptheme.Widget" parent="@android:style/Theme.Holo">
        <item name="android:popupMenuStyle">@style/PopupMenu.Apptheme</item>
        <item name="android:dropDownListViewStyle">@style/DropDownNavActionbar.Apptheme</item>
    </style>

    <style name="DropDownNavActionbar.Apptheme" parent="DropDownListView.Apptheme">
        <item name="android:listDivider">@android:color/white</item>
    </style>
</resources>

我被建议使用actionDropDownStyle,添加一个带有值@android:color / white的listDivider属性,但这不起作用。

1 个答案:

答案 0 :(得分:1)

添加这些样式

<style name="DropDownListView.ActionBar.Apptheme" parent="@style/DropDownListView.Apptheme">
    <item name="android:dividerHeight">1dp</item>
    <item name="android:divider">@android:color/white</item>
</style>

<style name="Theme.Apptheme.Widget.ActionBarSpinner">
    <item name="android:dropDownListViewStyle">@style/DropDownListView.ActionBar.Apptheme</item>
</style>

然后启用它:

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
Context themedContext = new ContextThemeWrapper(this, R.style.Theme_Apptheme_Widget_ActionBarSpinner);
Spinner navSpinner = new Spinner(themedContext);
navSpinner.setAdapter(new ActionBarSpinnerAdapter(themedContext, shoppingList));
navSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        Toast.makeText(MainActivity.this, "Item #" + position + " clicked", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent)
    {
        // do nothing
    }
});
actionBar.setCustomView(navSpinner);

如果需要,可以使用适配器代码:

private class ActionBarSpinnerAdapter extends BaseAdapter
{
    public static final int TYPE_CATEGORY = 0;
    public static final int TYPE_ITEM     = 1;
    public static final int MAX_TYPE      = 2;

    private LayoutInflater mInflater;
    private ArrayList<ShoppingListItem> mData;


    public ActionBarSpinnerAdapter(Context context, ArrayList<ShoppingListItem> data)
    {
        mInflater = LayoutInflater.from(context);
        mData = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final int type = getItemViewType(position);
        if(convertView == null){
            final int layoutId;
            switch(type){
                case TYPE_CATEGORY: layoutId = android.R.layout.preference_category; break;
                case TYPE_ITEM:     layoutId = android.R.layout.simple_list_item_1;  break;
                default: throw new IllegalArgumentException("Bad type: " + type);
            }
            convertView = mInflater.inflate(layoutId, parent, false);
        }

        TextView textView = (TextView)convertView;
        textView.setText(mData.get(position).getName());

        return convertView;
    }

    @Override
    public ShoppingListItem getItem(int position)
    {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return mData.get(position).getName().hashCode();
    }

    @Override
    public int getItemViewType(int position)
    {
        return mData.get(position).getType();
    }

    @Override
    public int getViewTypeCount()
    {
        return MAX_TYPE;
    }

    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }

    @Override
    public boolean isEnabled(int position)
    {
        return (getItemViewType(position) != TYPE_CATEGORY);
    }


    @Override
    public int getCount()
    {
        return mData.size();
    }
}