近兩天的工作涉及到了ActionBar中字體顏色的設置,目前找到兩種可以“應付”的方法,都不算太完善,日後可再深入研究。
第一種方法:
ActionBar的相關屬性設置是依賴於Application的style定義,因此可以直接在該style中修改相關屬性:
android:actionBarItemBackground:定義顯示的item選項的背景
android:actionBarDivider:定義item間的分隔符
android:actionMenuTextColor:定義menu item文字顏色
android:actionMenuTextAppearance:定義menu item文字樣式,這裏好像不能對textColor進行設置
android:actionBarTabTextStyle:定義Tab的文本樣式
還有其他的屬性,不一一列出………………
第二種方法:
在java文件中,對onCreateOptionMenu()的重寫中作修改,該方法還問研究透徹。這個方法的好處是相對靈活,針對不同的activity選擇性的改寫。
<pre name="code" class="java">Field field = LayoutInflater.class.getDeclaredField("mFactorySet);
field.setAccessible(true);
field.setBoolean(mInflater,false);
此段內容不合理,只是爲了不拋出A Factory has already been set on this LayoutInflater異常mInflater.setFactory(new Factory){
@Override
public View onCreateView(String name, Context context, AttributeSet attrs){
if(name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")
|| name.equalsIgnoreCase("com.android.internal.view.menu.ActionMenuItemView")){
final View view = mInflater.createView(name, null, attrs);
if(view instanceof TextView)
((TextView)view).setTextColor(Color.GRAY);
return view;
}
}
}