2013-05-02 104 views
7

我在我的操作栏中有一个导航列表,它具有黑色背景。然而,流行菜单具有白色背景。ActionBar列表导航:标题和弹出菜单中的不同文本颜色

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

这两个例子是我走到这一步:

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> 

然而没有什么不奏效。

+0

你是怎么做到这一点的?你可以发布一个工作代码吗? – 2013-09-11 06:13:09

+0

@ user2247689:目前我无法访问源代码。基本上,我从* matthias *提出的建议开始:应用两种不同的布局并为它们设置自定义样式(覆盖'textAppearance';请参阅我的评论)。我建议你看看ActionBarSherlock,因为你可以快速查找链接的样式源。后来我将我的实现更改为* Android-Developer *的解决方案,因为我的应用中有多个“Spinner”。 – Trinimon 2013-09-11 09:48:49

+0

感谢您的回复...我得到的东西工作:) – 2013-09-11 10:44:04

回答

12

问题是,您正在为微调器项目和微调器下拉项目使用相同的资源android.R.layout.simple_dropdown_item_1line。代替使用和R.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

这指出我进入正确的方向。我不知道正确的布局和(因此)适用的样式。我最终运行了上面的布局,并在主主题中添加了'spinnerItemStyle'和'spinnerDropDownItemStyle'。文本颜色和大小的实际变化可以通过派生的'textAppearance'样式完成。非常感谢你的帮助! – Trinimon 2013-05-06 09:40:19

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

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

+0

这个工程很好,对于那些标准布局不充分的情况,这绝对是一个有价值的提示。不过,我对这里的预定义布局很满意。感谢您的回复! – Trinimon 2013-05-06 09:23:15

+0

对于那些不想处理样式文件的人来说,这是一个很好的答案! – ryvianstyron 2013-07-22 20:28:09

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> 

应该是这样的。不知道为什么使用微调“物品”风格@@

+0

我正在使用ActionBarSherlock(请参阅上面的标签)。同时我已经实现了一个自定义视图,如_Android-Developer_所示。感谢您的回答,虽然:) – Trinimon 2013-12-04 07:21:30

+0

希望你能通过AppCompat主题风格得到一些提示 – 2014-03-11 04:39:04

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

} 
+0

不要忘记使用两个自定义布局。 – 2015-05-18 08:49:13