Android Action Bar not showing on displayOptions - useLogo

感情迁移 提交于 2019-12-04 18:01:29

If you set the android:logo in the theme you shouldn't need to set it in code.

I think you may also need showHome in your display options e.g.

<item name="android:displayOptions">showHome|useLogo</item>

EDIT

Below is an example theme I use to hide the logo and display the title in the action bar, this sample is using ActionBarSherlock so you will need to adjust the parent style names accordingly if you aren't using it.

<style name="AppTheme" parent="style/Theme.Sherlock.Light.DarkActionBar">
   <item name="android:windowBackground">@color/background_primary</item>
   <item name="android:icon">@drawable/appicon</item>    
   <!-- For the home as up icon on sub pages -->
   <item name="android:homeAsUpIndicator">@drawable/back</item>
   <item name="homeAsUpIndicator">@drawable/back</item>          
   <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>
   <item name="actionBarStyle">@style/AppTheme.ActionBar</item>
</style>

<style name="AppTheme.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="android:displayOptions">showTitle</item>
<item name="displayOptions">showTitle</item>

<item name="android:titleTextStyle">@style/AppTheme.ActionBar.TitleTextStyle</item>
<item name="titleTextStyle">@style/AppTheme.ActionBar.TitleTextStyle</item>    
<item name="android:background">@color/actionbar_bg_color</item>
<item name="background">@color/actionbar_bg_color</item>
<item name="android:backgroundStacked">@color/actionbar_bg_color</item>
<item name="backgroundStacked">@color/actionbar_bg_color</item>

This works from 4.2 api version.

In your ActionBar style add this…

<item name="android:displayOptions">showHome|useLogo</item>
<item name="displayOptions">showHome|useLogo</item>

<!-- Support library compatibility -->
<item name="android:logo">@drawable/ic_home</item>
<item name="logo">@drawable/ic_home</item>

Complete Example , adding a new Theme for your complete App.

<!-- This is the generated app theme -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<!-- general styles for the action bar -->
<style name="MyActionBar"
    parent="@style/Widget.AppCompat.ActionBar">

    <item name="android:displayOptions">showHome|useLogo</item>
    <!-- Support library compatibility -->
    <item name="displayOptions">showHome|useLogo</item>

    <item name="android:logo">@drawable/ic_home</item>
     <!-- Support library compatibility -->
    <item name="logo">@drawable/ic_home</item>
</style>

if you want use code to implement this effect, you can call below code from a Fragment.

ActionBarActivity activity = (ActionBarActivity) getActivity();
activity.getActionBar().setDisplayUseLogoEnabled(true);
activity.getActionBar().setDisplayShowHomeEnabled(true);

and you need to specify the logo attribute of the activity in the AndroidManifest.xml, for example,

    <activity
        android:name=".....MainEntryActivity"
        android:label="@string/label_main"
        android:screenOrientation="portrait"
        android:logo="@drawable/ic_ios_logo"
        android:theme="@style/AppCompatTheme" >
    </activity>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!