2016-08-19 30 views
3

在我家的活動,我有一個工具欄與自定義溢出菜單,我希望它改變顏色與工具欄背景顏色(被賦予原因工具欄背景顏色可以改變(用戶更改了自己最喜歡的顏色))...我不知道如何做到這一點:更改與Java代碼自定義子菜單的背景顏色

這裏是我的overflow_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 
<item android:id="@+id/overflow_save_current" app:showAsAction="always" android:title="@string/overflow_save_current" android:icon="@drawable/ic_action_save_light" /> 
<item android:id="@+id/overflow_settings" app:showAsAction="always" android:title="@string/overflow_settings" android:icon="@drawable/ic_action_settings_light" /> 
<item android:id="@+id/overflow_overflow" app:showAsAction="always" android:icon="@drawable/ic_action_overflow_light"> 
    <menu> 
     <item android:id="@+id/overflow_feed_back" app:showAsAction="never|withText" android:title="@string/overflow_feed_back" android:icon="@drawable/ic_action_send_now_light" /> 
     <item android:id="@+id/overflow_about_us" app:showAsAction="never|withText" android:title="@string/overflow_about_us" android:icon="@drawable/ic_action_about_light" /> 
     <item android:id="@+id/overflow_exit" app:showAsAction="never|withText" android:title="@string/overflow_exit" android:icon="@drawable/ic_action_forward_light" /> 
    </menu> 
</item> 

而這裏的OnPrepareOptionsMenu()方法:

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    /* Resources res = getResources(); 
    item = menu.findItem(R.id.overflow_feed_back); 
    SpannableStringBuilder builder = new SpannableStringBuilder("* " + res.getString(R.string.overflow_feed_back)); 
    builder.setSpan(new ImageSpan(this, android.R.drawable.ic_menu_send), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
    item.setTitle(builder); */ 
    if (!Common.compatible(Common.color, 0xFF000000)) { 
     menu.findItem(R.id.overflow_save_current).setIcon(R.drawable.ic_action_save_dark); 
     menu.findItem(R.id.overflow_settings).setIcon(R.drawable.ic_action_settings_dark); 
     menu.findItem(R.id.overflow_overflow).setIcon(R.drawable.ic_action_overflow_dark); 
    } else { 
     menu.findItem(R.id.overflow_save_current).setIcon(R.drawable.ic_action_save_light); 
     menu.findItem(R.id.overflow_settings).setIcon(R.drawable.ic_action_settings_light); 
     menu.findItem(R.id.overflow_overflow).setIcon(R.drawable.ic_action_overflow_light); 
    } 
    return super.onPrepareOptionsMenu(menu); 
} 

我已經試圖改變每個元素的背景顏色...但我每次NPE,與menu.findItem(R.id.overflow_about_us).getActionView().setBackgroundColor(Color.BLUE);

希望你會找到一個解決方案對我來說, Darkball60

回答

1

所以......沒人幫我......所以我不得不單獨工作......幾個小時...終於找到了解決方案:

@Override 
public View onCreateView(String name, Context context, AttributeSet attrs) { 
    // Do you own inflater stuff here 
    // Check for menu items (Options menu AKA menu key) 
    if (name.equalsIgnoreCase("android.support.v7.view.menu.ListMenuItemView")) { 
     try { 
      // Ask our inflater to create the view 
      final View view = LayoutInflater.from(context).createView(name, null, attrs); 
      // Kind of apply our own background 
      new Handler().post(new Runnable() { 
       public void run() { 
        if (!Common.compatible(Common.color, 0xFF000000)) { 
         try { 
          ((TextView)((RelativeLayout)((ListMenuItemView)view).getChildAt(1)).getChildAt(0)).setTextColor(0xFFFFFFFF); 
         } catch (ClassCastException e) { 

         } 
        } else { 
         try { 
          ((TextView)((RelativeLayout)((ListMenuItemView)view).getChildAt(1)).getChildAt(0)).setTextColor(0xFF000000); 
         } catch (ClassCastException e) { 

         } 
        } 
        view.setBackgroundColor(Common.color); 
       } 
      }); 
      return view; 
     } catch (InflateException e) { 

     } catch (ClassNotFoundException e) { 

     } 
    } 
    return null; 
} 

如果背景是黑暗的...所以textColor是白色的...如果背景是亮的,textColor是黑色的。

下面是兩個屏幕不同的工具欄的顏色:Red toolbarGreen toolbar

希望它可以幫助別人比我一樣,Darkball60(如果你喜歡的答案......隨意給予好評:))

+0

謝謝非常 !我很長一段時間尋找... –

+0

沒問題。實際上很難弄清楚如何直接在Java中做到這一點,以便能夠動態地改變它 – Mesabloo

相關問題