2013-05-02 3 views
7

アクションバーに暗い背景を持つナビゲーションリストがあります。しかし、ポップメニューは白い背景を持っています。ActionBarリストナビゲーション:ヘッダーとポップアップメニューのテキストの色が異なります

私が達成したいのは、メニューバーの項目のテキストの色が黒であるのに対して、アクションバーの中の項目のテキストの色は白です。

これは私がこれまでに得たものを2つの例です:

planned

誰もが解決策を知っています:

bad examples

これは、のようになります方法ですか?

これは、リストナビゲーションのための私のコードです:

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

どうやってこれを行いましたか?作業コードを投稿できますか? –

+0

@ user2247689:現在、私はソースにアクセスできません。基本的には、* matthias *が提案したものから始めました.2つの異なるレイアウトを適用し、カスタムスタイルを設定します( 'textAppearance'をオーバーライドしてください;私のコメントを参照してください)。私はあなたがActionBarSherlockを見てみることをお勧めします。なぜなら、リンクされたスタイルソースをすばやく見ることができるからです。後で私の実装を* Android-Developer *のソリューションに変更しました。これは私のアプリに複数のSpinnerがあるためです。 – Trinimon

+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のようなスタイルが機能します。

+3

これは正しい方向に私を指摘した。私は適切なレイアウトと適用するスタイルを認識していませんでした。私は最終的に上記のレイアウトで動作し、 'spinnerItemStyle'と' spinnerDropDownItemStyle'をメインテーマに追加しました。テキストの色とサイズの実際の変更は、派生した 'textAppearance'スタイルで行うことができます。あなたの助けをありがとう! – Trinimon

5

あなたのリストアイテムになるカスタム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" /> 

をし、このようにそれを使用する:このようなあなたのxmlを作成トリックを行う必要があります

adapter.setDropDownViewResource(R.layout.custom_list_item); 

は(少なくともそれは私のアプリケーションで作業しています)。

+0

これは素晴らしい作品ですが、特に標準的なレイアウトでは十分ではない場合には特に重要なヒントです。しかし、私はここであらかじめ定義されたレイアウトに満足しています。お返事をありがとうございます! – Trinimon

+0

スタイルファイルを扱いたくない人には朗報です! – ryvianstyron

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

+0

希望AppCompatのテーマスタイル –

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

でいくつかのヒントを得ることができる2つのカスタムレイアウトを使用することを忘れないでください。 –

関連する問題