v7-21 ActionBarActivity not showing the app main icon on the left

孤人 提交于 2020-01-01 18:23:59

问题


I implemented a basic activity as

public class MainActivity extends Activity

and when I add an actionbar with the menu as below, the app icon defined in the AndroidManifest.xml shows up nicely

<application
android:allowBackup="true"
android:icon="@drawable/logo_green"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >

When I try to change to android.support.v7.app.ActionBarActivity ( and switch to compatible AppCompat theme like @style/Theme.AppCompat.Light.DarkActionBar) the main app menu in the action bar strangely disappears

I tried getting it back to where it should be in the Oncreate() method but it does not work

actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setIcon(R.drawable.green_drawable);

action bar xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <!-- Search / will display always -->


    <item android:id="@+id/notification"
        android:icon="@drawable/notification_active"
        android:title="Setup"
        app:showAsAction="never"/>

    <item android:id="@+id/action_search"
        android:icon="@drawable/add_friends"
        android:title="Add Friends"
        app:showAsAction="never"/>


    <item android:id="@+id/action_setup"
        android:icon="@drawable/trackingicon"
        android:title="Setup"
        app:showAsAction="never"/>

    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never" />

    <item android:id="@+id/action_help"
        android:icon="@drawable/helpicon"
        android:title="Contact Support"
        app:showAsAction="ifRoom"/>


</menu>

回答1:


setIcon/setLogo method will only work if you have set DisplayOptions Try this -

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setIcon(R.drawable.ic_launcher);

You can also set options for displaying LOGO(just add constant ActionBar.DISPLAY_USE_LOGO). More information - displayOptions



来源:https://stackoverflow.com/questions/27612424/v7-21-actionbaractivity-not-showing-the-app-main-icon-on-the-left

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