简体   繁体   中英

Anyone knows how to set a custom action bar?

Anyone knows how to set a green action bar with the logo of my app and the name of the app, i've tried it but it appears white with no logo.

My MainActivity extends AppCompatActivity. Thats why I need to use Theme.AppCompat in Manifest.xml

Here's style.XML

<style name="MyTheme" parent="Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>">

</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@color/colorVerd</item>
    <item name="android:textColor">@color/colorBlanc</item>
</style>

My android manifest:

<?xml version="1.0" encoding="utf-8"?>

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

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

    </activity>
    <activity
        android:name=".Main2Activity"
        android:label="@string/title_activity_main2">
        <intent-filter>
        <action android:name="android.intent.action.MAIN"/>


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

    </activity>
</application>
<receiver android:name=".Alarm_Receiver1"/>
<receiver android:name=".Alarm_Receiver2"/>

Your activity is an AppCompatActivity so you should use an appcompat theme as well,To change your toolbar attributes override toolbarStyle , Modify the styles.xml file like this:

 <style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="toolbarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@color/colorVerd</item>
    <item name="titleTextColor">@color/Blanc</item>
</style>

Update

In order to show your desired logo use these lines of code in onCreate method:

    toolbar = getSupportActionBar();
    toolbar.setDisplayUseLogoEnabled(true);
    toolbar.setDisplayShowHomeEnabled(true);
    toolbar.setLogo(R.drawable.logo);

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