简体   繁体   中英

Styling Action Bar(Background Color) Android

I want to make the action bar red and it is not working. It launches the app and then immediately says "Unfortunately, Action Bar has stopped" and I can see the app real quick and it is read. I looked for hours and nothing has come up on the internet. Can someone please provide help, since I am new to coding?

logcat

      05-29 01:04:39.051
 21993-21993funnestgames.actionbarActivityThread:PauseGC
 java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
       at android.app.ActivityThread.access$900(ActivityThread.java:175)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5602)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)
      Caused by: java.lang.UnsatisfiedLinkError: Native method not     
        found:dalvik.system.VMRuntime.pauseGc:(Ljava/lang/String;)I
        at dalvik.system.VMRuntime.pauseGc(Native Method)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
        at android.app.ActivityThread.access$900(ActivityThread.java:175)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5602)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)

styl.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
    parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
    parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#F00</item>
</style>
</resources>

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="funnestgames.actionbar" >

<application
    android:theme="@style/CustomActionBarTheme"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

Use this code below

ActionBar action_bar = getActionBar();
action_bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF0000")));

Change your style from

<!--
    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="Theme.AppCompat.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="CustomActionBarTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

Try this

<style name="ActionBar.Solid.Example" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
        <item name="android:background">Your color you want or drawable</item>
        <item name="android:backgroundStacked">Your color you want or drawable</item>
        <item name="android:backgroundSplit">Your color you want or drawable</item>
</style>

And add this on AppTheme your app:

<item name="android:actionBarStyle">@style/ActionBar.Solid.Example</item>

Or try use tools

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM