操作栏列表导航:标题和弹出菜单中文字颜色不同。

7

我在我的操作栏中有一个导航列表,它具有深色背景。但是弹出菜单具有白色背景。

因此,我想要实现的是,在操作栏内的项目文本颜色为白色,而菜单弹出窗口中的项目文本颜色为黑色。

这是两个已经得到的示例:

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: https://dev59.com/PGjWa4cB1Zd3GeqPqGEk           
      <item name="android:spinnerDropDownItemStyle">@style/custom.Widget.DropDownItem.Spinner</item>
      <item name="spinnerDropDownItemStyle">@style/custom.Widget.DropDownItem.Spinner</item>
      -->

      <!-- didn't work: https://dev59.com/ymgu5IYBdhLWcg3wOUq- 
      <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>

然而,没有任何东西起作用。

你是怎么做到的?能否发一个可运行的代码? - Rakeeb Rajbhandari
@user2247689:目前我无法访问源代码。基本上,我开始使用matthias提出的方法:应用两种不同的布局并为它们设置自定义样式(覆盖textAppearance;请参见我的评论)。我建议您查看ActionBarSherlock,因为您可以快速查找链接的样式源。后来,我将我的实现更改为Android-Developer的解决方案,因为我的应用程序中有多个Spinner - Trinimon
谢谢回复...我已经让它正常工作了 :) - Rakeeb Rajbhandari
4个回答

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这样的样式就会生效。


3
这让我找到了正确的方向。我之前不知道应该使用哪种布局和风格,因此无法实现。最终,我通过使用特定布局并将 spinnerItemStylespinnerDropDownItemStyle 添加到主题中使其成功运行。文字颜色和大小的实际更改可以在派生的 textAppearance 样式中完成。非常感谢你的帮助! - Trinimon

5
您可以通过创建自定义的 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);

那应该就可以解决问题了(至少在我的应用程序中它是有效的)。

这个很好用,对于那些标准布局不足以满足需求的情况来说,这绝对是一个有价值的提示。然而,在这里我很满意预定义的布局。感谢您的回复! - Trinimon
对于那些不想处理样式文件的人来说,这是一个很好的答案! - shecodesthings

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"样式 @@

我正在使用ActionBarSherlock(请参见上面的标签)。与此同时,我已经按照_Android-Developer_的建议实现了自定义视图。不过还是谢谢你的回答 :) - Trinimon
希望你能通过 AppCompat 主题样式获得一些提示。 - Shawn Thye

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

}

不要忘记使用两个自定义布局。 - Ye Tun Thu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接