7

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

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

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

不好的例子

它应该是这样的:

计划

有谁知道解决方案?

这是我的列表导航代码:

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

4 回答 4

12

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

使用R.layout.sherlock_spinner_itemandR.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就会起作用。

于 2013-05-05T12:39:13.177 回答
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);

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

于 2013-05-05T12:46:36.003 回答
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>

应该是这样的。不知道为什么要用 spinner"Item"Style @@

于 2013-12-03T15:27:31.870 回答
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;
    }

}
于 2015-05-18T08:47:30.077 回答