我实现了一个基本活动为
公共类MainActivity扩展活动
和我添加一个动作条有如下菜单,在AndroidManifest.xml中定义的应用程序图标显示很好
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ logo_green
机器人:标签=@字符串/ APP_NAME
机器人:screenOrientation =画像
机器人:主题=@风格/ Theme.AppCompat.Light.DarkActionBar>
当我尝试改变android.support.v7.app.ActionBarActivity(并切换到兼容的AppCompat主题像@风格/ Theme.AppCompat.Light.DarkActionBar)在操作栏上的主要的应用程序菜单奇怪消失了。
我试图得到它回到它应该在的OnCreate()方法,但它不能正常工作
动作条= getSupportActionBar();
actionBar.setHomeButtonEnabled(真正的);
actionBar.setIcon(R.drawable.green_drawable);
< XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto>
< - !搜索/总是显示 - >
<项目机器人:ID =@ + ID /通知
机器人:图标=@可绘制/ notification_active
机器人:标题=设置
应用程序:showAsAction =从不/>
<项目机器人:ID =@ + ID / ACTION_SEARCH
机器人:图标=@可绘制/ add_friends
机器人:标题=加为好友
应用程序:showAsAction =从不/>
<项目机器人:ID =@ + ID / action_setup
机器人:图标=@可绘制/ trackingicon
机器人:标题=设置
应用程序:showAsAction =从不/>
<项目机器人:ID =@ + ID / action_settings
机器人:标题=@字符串/ action_settings
应用程序:showAsAction =从不/>
<项目机器人:ID =@ + ID / action_help
机器人:图标=@可绘制/ helpicon
机器人:标题=联系支持
应用程序:showAsAction =ifRoom/>
< /菜单>
解决方案
的setIcon / setLogo
如果设置方法仅适用 DisplayOptions 尝试这一点 -
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setIcon(R.drawable.ic_launcher);
您还可以设置选项来显示的LOGO(刚加入恒 ActionBar.DISPLAY_USE_LOGO
)。更多信息 - displayOptions
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);
<?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>
解决方案
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