Android Action Bar not showing on displayOptions - useLogo

大憨熊 提交于 2019-12-06 11:03:34

问题


I have an actionBarStyle that I have implemented in my app for my ActionBar. Here is the xml code below:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="MyActionBarTheme" parent="@android:style/Theme.Holo">
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:actionBarStyle">@style/MyCustomActionBar</item>
    </style>
    <style name="MyCustomActionBar" parent="@android:style/Widget.Holo.ActionBar">
        <item name="android:background">@android:color/transparent</item>
        <item name="android:backgroundSplit">@android:color/transparent</item>
        <item name="android:displayOptions">useLogo</item>
    </style>

And here is my Activity OnCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setLogo(R.drawable.ic_logo);
    setContentView(R.layout.activity_home);

    initializeViews();
    manageFragments();
}

The Problem is that my ActionBar doesn't show at all and when I remove the displayOptions item from My XML. It shows with the title. Now I can always remove the title (set the title string to just blank) but I don't want that as it shows the default ActionBar style/theme for a few seconds and then my custom theme (I guess you know this one). I don't know what the problem is. Please help. Thanks...


回答1:


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>




回答2:


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>



回答3:


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>


来源:https://stackoverflow.com/questions/20428261/android-action-bar-not-showing-on-displayoptions-uselogo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!