ActionBar列表导航:标题和弹出菜单中的不同文本颜色

时间:2013-05-02 13:06:38

标签: android android-actionbar actionbarsherlock

我的操作栏中有一个导航列表,背景为深色。然而,弹出菜单有白色背景。

所以我想要实现的是,操作栏内的项目文本颜色是白色,而菜单弹出的项目文本颜色是黑色。

这是我到目前为止的两个例子:

bad examples

这应该是这样的:

planned

有谁知道解决方案?

这是我的列表导航代码:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_dropdown_item_1line, new String[] { "Item 1", "Item 2" });

getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(adapter,
        new ActionBar.OnNavigationListener() {
            @Override
            public boolean onNavigationItemSelected(int itemPosition, long itemId) {
                return true;
            }
        });

getSupportActionBar().setSelectedNavigationItem(0)

这些是我使用的样式集合。

<style name="CustomTheme" parent="@style/Theme.customized">
      <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
      <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
      <item name="actionDropDownStyle">@style/CustomSherlockDropDownNav</item>
      <item name="android:actionDropDownStyle">@style/CustomSherlockDropDownNav</item>

      <!-- didn't work: http://stackoverflow.com/questions/12395381/android-actionbar-navigation-spinner-text-color           
      <item name="android:spinnerDropDownItemStyle">@style/custom.Widget.DropDownItem.Spinner</item>
      <item name="spinnerDropDownItemStyle">@style/custom.Widget.DropDownItem.Spinner</item>
      -->

      <!-- didn't work: http://stackoverflow.com/questions/11479186/styling-actionbar-dropdown-menu 
      <item name="android:actionBarWidgetTheme">@style/custom.actionBarWidgetTheme</item>
      <item name="actionBarWidgetTheme">@style/custom.actionBarWidgetTheme</item>
      -->

      <!-- didn't work: http://android-developers.blogspot.de/2011/04/customizing-action-bar.html                                                                        
      <item name="android:dropDownListViewStyle">@style/CustomDropDownListView</item>
      <item name="dropDownListViewStyle">@style/CustomDropDownListView</item>
      -->

      ....  
</style>


<style name="custom.actionBarWidgetTheme" parent="@style/Theme.Sherlock.Light.DarkActionBar">
     <item name="android:spinnerDropDownItemStyle">@style/custom.Widget.DropDownItem.Spinner</item>
</style>

<style name="custom.Widget.DropDownItem.Spinner" parent="@style/Widget.Sherlock.DropDownItem.Spinner">
    <item name="android:textAppearance">@style/custom.TextAppearance.Widget.DropDownItem</item>
</style>

<style name="custom.TextAppearance.Widget.DropDownItem" parent="@style/TextAppearance.Sherlock.Widget.DropDownItem">
    <item name="android:textColor">#00A000</item>
</style>


<style name="CustomDropDownListView" parent="android:style/Widget.Holo.ListView.DropDown">
  <item name="android:textColor">#00A000</item>
  <item name="android:textSize">8dip</item>
</style>


<style name="CustomSherlockDropDownNav" parent="@style/Widget.Sherlock.Light.Spinner.DropDown.ActionBar">
      <item name="android:popupBackground">@drawable/menu_dropdown_panel_customtab</item>
      <item name="android:background">@drawable/spinner_background_ab_customtab</item>
</style>

然而没有什么不起作用。

4 个答案:

答案 0 :(得分:12)

问题是您对微调器项和微调器下拉项使用相同的资源android.R.layout.simple_dropdown_item_1line

改为使用R.layout.sherlock_spinner_itemR.layout.sherlock_spinner_dropdown_item

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
         R.layout.sherlock_spinner_item, new String[] { "Item 1", "Item 2" });
adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

这样,Widget.Sherlock.TextView.SpinnerItem等样式就可以了。

答案 1 :(得分:5)

您只需创建自定义xml即可实现此目标,该xml将成为您的列表项。像这样创建你的xml: 的 custom_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:textSize="18sp"
    android:textColor="@color/holo_dark_red"
    android:paddingRight="110dip"
    android:layout_width="match_parent"
    android:layout_height="?attr/dropdownListPreferredItemHeight"
    android:textIsSelectable="false"
    android:ellipsize="marquee" />

并像这样使用它:

adapter.setDropDownViewResource(R.layout.custom_list_item);

应该这样做(至少它在我的应用程序中工作)。

答案 2 :(得分:1)

<style name="Theme.WhyCheck" parent="@style/Theme.AppCompat.Light">
<item name="android:spinnerItemStyle">@style/DropDownNav.Item.Inverse</item>
</style>

<style name="DropDownNav.Item.Inverse" parent="@style/Widget.AppCompat.DropDownItem.Spinner">
    <item name="android:textAppearance">@style/ActionBar.TitleText</item>
</style>

<style name="ActionBar.TitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textColor">@color/white</item>
</style>

应该是这样的。不知道为什么使用微调器“Item”Style @@

答案 3 :(得分:0)

         @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        // Inflating the layout for the custom Spinner
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom, null);

        // Declaring and Typecasting the textview in the inflated layout
        TextView tvLanguage = (TextView) layout
        .findViewById(R.id.tvLanguage);

        // Setting the text using the array
        tvLanguage.setText(obj[position]);

        //tvLanguage.setTextColor(Color.BLACK);
        tvLanguage.setTextSize(14f);




        return layout;

    }
    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub


        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.customdropdown, null);

        // Declaring and Typecasting the textview in the inflated layout
        TextView tvLanguage = (TextView) layout
        .findViewById(R.id.tvLanguage);

        // Setting the text using the array
        tvLanguage.setText(obj[position]);

        //tvLanguage.setTextColor(Color.BLACK);
        tvLanguage.setTextSize(14f);



        return layout;
    }

}