如何更改操作栏背景和文本颜色 [英] How to change action bar background and text colors

查看:140
本文介绍了如何更改操作栏背景和文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的抽屉式导航从本教程

http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

和我试图改变动作栏背景从这个选择的答案

设置标题背景颜色

但是当我跑我的应用程序的应用程序有崩溃,并显示错误

清单

 <应用
    机器人:allowBackup =真
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
    <活动
        机器人:名称=info.androidhive.slidingmenu.MainActivity
        机器人:标签=@字符串/ APP_NAME>

    < /活性GT;

    <活动机器人:名称=Login_Page
        机器人:主题=@安卓风格/ Theme.Black.NoTitleBar.Fullscreen
        >


    < /活性GT;

    <活动机器人:名称=注册>< /活性GT;
    <活动机器人:名称=SplashActivity
                    机器人:主题=@安卓风格/ Theme.Black.NoTitleBar.Fullscreen

        >
         <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>

            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>

    < /活性GT;
< /用途>
 

MainActivity

  @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        的setContentView(R.layout.activity_main);
        。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.mytitle);
 

LogCat中

  

12月7号至1日:57:38.882:E / AndroidRuntime(14690):致命异常:主要   十二月七号至一号:57:38.882:E / AndroidRuntime(14690):   java.lang.RuntimeException的:无法启动的活动   ComponentInfo {info.androidhive.slidingmenu / info.androidhive.slidingmenu.MainActivity}:   android.util.AndroidRuntimeException:不能将自定义标题   与其他标题功能12月七号至1日:57:38.882:E / AndroidRuntime(14690):     在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)   十二月七号至一号:57:38.882:E / AndroidRuntime(14690):在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)   十二月七号至一号:57:38.882:E / AndroidRuntime(14690):在   android.app.ActivityThread.access $ 600(ActivityThread.java:128)07-01   12:57:38.882:E / AndroidRuntime(14690):在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1161)   十二月七号至一号:57:38.882:E / AndroidRuntime(14690):在   android.os.Handler.dispatchMessage(Handler.java:99)07-01   12:57:38.882:E / AndroidRuntime(14690):在   android.os.Looper.loop(Looper.java:137)12月7日至1号:57:38.882:   E / AndroidRuntime(14690):在   android.app.ActivityThread.main(ActivityThread.java:4517)07-01   12:57:38.882:E / AndroidRuntime(14690):在   java.lang.reflect.Method.invokeNative(本机方法)07-01   12:57:38.882:E / AndroidRuntime(14690):在   java.lang.reflect.Method.invoke(Method.java:511)七月一号12:57:38.882:   E / AndroidRuntime(14690):在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:993)   十二月七号至一号:57:38.882:E / AndroidRuntime(14690):在   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)07-01   12:57:38.882:E / AndroidRuntime(14690):在   dalvik.system.NativeStart.main(本机方法)12月7号至1日:57:38.882:   E / AndroidRuntime(14690):产生的原因:   android.util.AndroidRuntimeException:不能将自定义标题   与其他标题功能12月七号至1日:57:38.882:E / AndroidRuntime(14690):     在   com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:245)   十二月七号至一号:57:38.882:E / AndroidRuntime(14690):在   com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2701)   十二月七号至一号:57:38.882:E / AndroidRuntime(14690):在   com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2921)   十二月七号至一号:57:38.882:E / AndroidRuntime(14690):在   com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)   十二月七号至一号:57:38.882:E / AndroidRuntime(14690):在   android.app.Activity.setContentView(Activity.java:1835)07-01   12:57:38.882:E / AndroidRuntime(14690):在   info.androidhive.slidingmenu.MainActivity.onCreate(MainActivity.java:50)   十二月七号至一号:57:38.882:E / AndroidRuntime(14690):在   android.app.Activity.performCreate(Activity.java:4470)07-01   12:57:38.882:E / AndroidRuntime(14690):在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)   十二月七号至一号:57:38.882:E / AndroidRuntime(14690):在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)   07-01 12:57:38.882:E / AndroidRuntime(14690):...... 11多个07-01   12:57:38.892:E / AlarmManagerService(378):   android_server_AlarmManagerService_set为type = 3,51461.572000000   十二月七号至一号:57:38.892:V / AlarmManager(378):waitForAlarm结果:8月7日至1日   12:57:38.902:E / android.os.Debug(378):@Dumpstate> dumpstate -k -t   -n -z -d -o /数据/日志/ dumpstate_app_error

Style.xml

 <资源的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <样式名称=WindowTitleBackground>
        <项目名称=机器人:背景> @色/ titlebackgroundcolor< /项目>
        <项目名称=机器人:文字颜色> @色/ titletextcolor< /项目>
    < /风格>
    <! - 
        基本应用的主题,依赖于API级别。这个主题被替换
        通过AppBaseTheme从RES /值-VXX /上较新的设备styles.xml。
     - >
    <样式名称=AppBaseTheme父=机器人:Theme.Light>
        <! - 
            在新的API级别可以在可用主题自定义
            RES /值-VXX / styles.xml,而自定义相关
            向后兼容性可以去这里。
         - >
    < /风格>

    <! - 应用主题。 - >
    <样式名称=AppTheme父=AppBaseTheme>
        <! - 所有的定制,这不是针对特定的API级别的可以去这里。 - >
    < /风格>



< /资源>
 

解决方案

在试试这个 styles.xml

 <资源的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <! - 
        基本应用的主题,依赖于API级别。这个主题被替换
        通过AppBaseTheme从RES /值-VXX /上较新的设备styles.xml。


     - >
    <样式名称=AppBaseTheme父=机器人:Theme.Light>
        <! - 
            在新的API级别可以在可用主题自定义
            RES /值-VXX / styles.xml,而自定义相关
            向后兼容性可以去这里。


         - >
    < /风格>

    <! - 应用主题。 - >
    <样式名称=AppTheme父=AppBaseTheme>
        <项目名称=机器人:actionBarStyle> @风格/ MyActionBar< /项目>
    < /风格>

    <样式名称=MyActionBar父=@安卓风格/ Widget.Holo.Light.ActionBar>
        <项目名称=机器人:背景> @色/ titlebackgroundcolor< /项目>
        <项目名称=机器人:titleTextStyle> @风格/ MyActionBar.Text< /项目>
    < /风格>

    <样式名称=MyActionBar.Text父=@安卓风格/ TextAppearance.Holo.Widget.ActionBar.Title>
        <项目名称=机器人:文字颜色>#FFFF00< /项目>
    < /风格>

< /资源>
 

删除这两行:

  requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.mytitle);
 

这会为你工作。

I am using navigation drawer from this tutorial

http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

and i am trying to change action bar background from this selected answer

Set title background color

but when i run my app app got crash and shows error

Manifest

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="info.androidhive.slidingmenu.MainActivity"
        android:label="@string/app_name" >

    </activity>

    <activity android:name="Login_Page"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
        >


    </activity>

    <activity android:name="Register"></activity>
    <activity android:name="SplashActivity"
                    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

        >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>        
</application>

MainActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);

LogCat

07-01 12:57:38.882: E/AndroidRuntime(14690): FATAL EXCEPTION: main 07-01 12:57:38.882: E/AndroidRuntime(14690): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivity}: android.util.AndroidRuntimeException: You cannot combine custom titles with other title features 07-01 12:57:38.882: E/AndroidRuntime(14690): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970) 07-01 12:57:38.882: E/AndroidRuntime(14690): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) 07-01 12:57:38.882: E/AndroidRuntime(14690): at android.app.ActivityThread.access$600(ActivityThread.java:128) 07-01 12:57:38.882: E/AndroidRuntime(14690): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161) 07-01 12:57:38.882: E/AndroidRuntime(14690): at android.os.Handler.dispatchMessage(Handler.java:99) 07-01 12:57:38.882: E/AndroidRuntime(14690): at android.os.Looper.loop(Looper.java:137) 07-01 12:57:38.882: E/AndroidRuntime(14690): at android.app.ActivityThread.main(ActivityThread.java:4517) 07-01 12:57:38.882: E/AndroidRuntime(14690): at java.lang.reflect.Method.invokeNative(Native Method) 07-01 12:57:38.882: E/AndroidRuntime(14690): at java.lang.reflect.Method.invoke(Method.java:511) 07-01 12:57:38.882: E/AndroidRuntime(14690): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 07-01 12:57:38.882: E/AndroidRuntime(14690): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 07-01 12:57:38.882: E/AndroidRuntime(14690): at dalvik.system.NativeStart.main(Native Method) 07-01 12:57:38.882: E/AndroidRuntime(14690): Caused by: android.util.AndroidRuntimeException: You cannot combine custom titles with other title features 07-01 12:57:38.882: E/AndroidRuntime(14690): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:245) 07-01 12:57:38.882: E/AndroidRuntime(14690): at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2701) 07-01 12:57:38.882: E/AndroidRuntime(14690): at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2921) 07-01 12:57:38.882: E/AndroidRuntime(14690): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) 07-01 12:57:38.882: E/AndroidRuntime(14690): at android.app.Activity.setContentView(Activity.java:1835) 07-01 12:57:38.882: E/AndroidRuntime(14690): at info.androidhive.slidingmenu.MainActivity.onCreate(MainActivity.java:50) 07-01 12:57:38.882: E/AndroidRuntime(14690): at android.app.Activity.performCreate(Activity.java:4470) 07-01 12:57:38.882: E/AndroidRuntime(14690): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053) 07-01 12:57:38.882: E/AndroidRuntime(14690): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934) 07-01 12:57:38.882: E/AndroidRuntime(14690): ... 11 more 07-01 12:57:38.892: E/AlarmManagerService(378): android_server_AlarmManagerService_set to type=3, 51461.572000000 07-01 12:57:38.892: V/AlarmManager(378): waitForAlarm result :8 07-01 12:57:38.902: E/android.os.Debug(378): !@Dumpstate > dumpstate -k -t -n -z -d -o /data/log/dumpstate_app_error

Style.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="WindowTitleBackground">     
        <item name="android:background">@color/titlebackgroundcolor</item>   
        <item name="android:textColor">@color/titletextcolor</item>                
    </style>
    <!--
        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="android:Theme.Light">
        <!--
            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">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>



</resources>

解决方案

Try this in 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="android:Theme.Light">
        <!--
            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/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/titlebackgroundcolor</item>
        <item name="android:titleTextStyle">@style/MyActionBar.Text</item>
    </style>

    <style name="MyActionBar.Text" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">#FFFF00</item>
    </style>

</resources>

Remove these two lines:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);

It will work for you.

这篇关于如何更改操作栏背景和文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆