设置操作栏项目文本颜色?

时间:2014-07-21 07:45:21

标签: android xml android-actionbar android-styles

我正在尝试使用自定义style设置操作栏项目的文字颜色,但它没有效果。

我尝试过设置:

<item name="android:actionMenuTextColor">@style/AppTheme.ActionBar.Text</item>

<style name="ActionBar"无效的<style name="AppTheme"07-21 13:12:58.519: E/AndroidRuntime(24690): FATAL EXCEPTION: main 07-21 13:12:58.519: E/AndroidRuntime(24690): android.view.InflateException: Binary XML file line #17: Error inflating class com.android.internal.view.menu.ActionMenuItemView 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.view.LayoutInflater.createView(LayoutInflater.java:619) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.view.LayoutInflater.inflate(LayoutInflater.java:467) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.view.menu.BaseMenuPresenter.createItemView(BaseMenuPresenter.java:157) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.view.menu.BaseMenuPresenter.getItemView(BaseMenuPresenter.java:176) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.view.menu.ActionMenuPresenter.getItemView(ActionMenuPresenter.java:306) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.view.menu.ActionMenuPresenter.flagActionItems(ActionMenuPresenter.java:593) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.view.menu.MenuBuilder.flagActionItems(MenuBuilder.java:1048) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:91) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.view.menu.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:334) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:244) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:946) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.view.menu.MenuBuilder.startDispatchingItemsChanged(MenuBuilder.java:969) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:507) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:851) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:258) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.view.Choreographer.doFrame(Choreographer.java:531) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.os.Handler.handleCallback(Handler.java:725) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.os.Handler.dispatchMessage(Handler.java:92) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.os.Looper.loop(Looper.java:137) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.app.ActivityThread.main(ActivityThread.java:5328) 07-21 13:12:58.519: E/AndroidRuntime(24690): at java.lang.reflect.Method.invokeNative(Native Method) 07-21 13:12:58.519: E/AndroidRuntime(24690): at java.lang.reflect.Method.invoke(Method.java:511) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 07-21 13:12:58.519: E/AndroidRuntime(24690): at dalvik.system.NativeStart.main(Native Method) 07-21 13:12:58.519: E/AndroidRuntime(24690): Caused by: java.lang.reflect.InvocationTargetException 07-21 13:12:58.519: E/AndroidRuntime(24690): at java.lang.reflect.Constructor.constructNative(Native Method) 07-21 13:12:58.519: E/AndroidRuntime(24690): at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.view.LayoutInflater.createView(LayoutInflater.java:593) 07-21 13:12:58.519: E/AndroidRuntime(24690): ... 29 more 07-21 13:12:58.519: E/AndroidRuntime(24690): Caused by: android.content.res.Resources$NotFoundException: Resource is not a ColorStateList (color or path): TypedValue{t=0x1/d=0x7f0c008f a=3 r=0x7f0c008f} 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.content.res.Resources.loadColorStateList(Resources.java:2963) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.content.res.TypedArray.getColorStateList(TypedArray.java:343) 07-21 13:12:58.519: E/AndroidRuntime(24690): at android.widget.TextView.<init>(TextView.java:851) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.view.menu.ActionMenuItemView.<init>(ActionMenuItemView.java:116) 07-21 13:12:58.519: E/AndroidRuntime(24690): at com.android.internal.view.menu.ActionMenuItemView.<init>(ActionMenuItemView.java:112) 。如果我做后者,我的应用程序崩溃:

android:itemTextAppearance

我也尝试使用styles.xml,这也没有效果。

这是我的<resources xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <item name="android:actionBarStyle">@style/ActionBar</item> <item name="android:actionBarTabTextStyle">@style/ActionBarTabText</item> <item name="android:itemTextAppearance">@style/TextAppearance</item> <item name="actionBarStyle">@style/ActionBar</item> <item name="actionBarTabTextStyle">@style/ActionBarTabText</item> </style> <!-- Action Bar --> <style name="ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar"> <item name="android:titleTextStyle">@style/AppTheme.ActionBar.TitleTextStyle</item> <item name="android:actionMenuTextColor">@style/AppTheme.ActionBar.Text</item> <item name="android:background">@color/Black</item> <item name="titleTextStyle">@style/AppTheme.ActionBar.TitleTextStyle</item> <item name="actionMenuTextColor">@style/AppTheme.ActionBar.Text</item> </style> <style name="AppTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance"> <item name="android:textColor">@color/White</item> </style> <style name="AppTheme.ActionBar.Text" parent="@android:style/TextAppearance"> <item name="android:textColor">@color/White</item> </style> <!-- Action Bar Tabs --> <style name="AppTheme.Tabtheme" parent="@style/Theme.AppCompat.Light.DarkActionBar"> <item name="android:actionBarTabTextStyle">@style/ActionBarTabText</item> <item name="actionBarTabTextStyle">@style/ActionBarTabText</item> </style> <style name="ActionBarTabText" parent="@style/Widget.AppCompat.Base.ActionBar.TabText"> <item name="android:textColor">@color/White</item> </style> <!-- Text color for Action bar items --> <style name="TextAppearance"> <item name="android:textColor">@android:color/white</item> </style> </resources>

{{1}}

1 个答案:

答案 0 :(得分:1)

您可以通过设置Android操作栏的自定义视图轻松完成..请按照以下

首先像这样设置ActionBar的自定义视图..

actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.action_provider);

现在将文本视图放在布局“action_provider”中。

现在获取你的textview id

pack_tv = (TextView) actionBar.getCustomView().findViewById(R.id.action_bar_tv);

现在你可以轻松地做任何你想做的事情..在你的情况下设置文本视图颜色..像这样

pack_tv.setTextColor("your_text_color");

你可以做任何事情..请检查以下代码..

actionBar = getSupportActionBar();
        actionBar.setCustomView(R.layout.action_provider);
        iv_d = (ImageView) actionBar.getCustomView().findViewById(R.id.action_menu);
        pack_tv = (TextView) actionBar.getCustomView().findViewById(R.id.action_bar_tv);
        spinner_value = (TextView) actionBar.getCustomView().findViewById(R.id.action_city);
        city_spinner = (LinearLayout) actionBar.getCustomView().findViewById(R.id.city_spinner_layout);
        pack_spinner = (LinearLayout) actionBar.getCustomView().findViewById(R.id.pack_spinner_layout);
        dashiv1 = (ImageView) actionBar.getCustomView().findViewById(R.id.dash_iv1);
        dashiv2 = (ImageView) actionBar.getCustomView().findViewById(R.id.dash_iv2);

        dashiv1.setVisibility(View.GONE);
        dashiv2.setVisibility(View.GONE);

        iv_d.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });

就是这样......你很高兴。