我正在尝试自定义ActionBar
,以便我可以添加名为" scan"的项目,当我运行以下代码时ActionBar
上没有显示任何内容。
请让我知道如何正确地做到这一点。
更新:
我的活动扩展了AvtionBarActivity
码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
menu.findItem(R.id.itemDiscover).setVisible(true);
return super.onCreateOptionsMenu(menu);
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/itemDiscover"
android:title="Scan"
android:icon="@drawable/fr"
android:showAsAction="always"/>
</menu>
style.xml :
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
答案 0 :(得分:0)
试用此代码并为您的菜单添加额外的xmlns
属性,然后尝试使用app:showAsAction
代替android:showAsAction
<?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" >
<item
android:id="@+id/itemDiscover"
android:title="Scan"
android:icon="@drawable/fr"
app:showAsAction="always"/>
</menu>
我希望它有所帮助!
答案 1 :(得分:0)
使用
更改menu.xml<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/itemDiscover"
android:title="Scan"
android:icon="@drawable/fr"
android:showAsAction="always"/>
</menu>