[英]android sherlock actionbar menu item going wrong
as the title suggest, I am getting a problem with the action Bar since i have recently installed android Api 22 under eclipse and jdk-7u76-windows-i586. 如标题所示,由于我最近在eclipse和jdk-7u76-windows-i586下安装了android Api 22,因此操作栏出现问题。 Before the action Bar keep to display the icon with sdk version 7 and higher.
在操作栏之前,请继续显示sdk版本7及更高版本的图标。 But now icon is displayed only with sdk version 11 and higher,did I miss something or am I just blind?, here are the menu XML file and a screen shot:
但是现在仅在sdk 11及更高版本中显示图标,是我错过了某些东西还是只是瞎了?这是菜单XML文件和屏幕截图:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/share"
android:icon="@drawable/ic_action_add_group"
android:title="share info"
android:showAsAction="ifRoom" />
Good i finally find the answer. 好,我终于找到答案了。 But what is strange is that the code use to work before the sdk update:Means that this newer version of sdk has a good improvement about XML files.
但是奇怪的是,该代码在sdk更新之前可以正常工作:意味着此新版本的sdk对XML文件有很好的改进。 Then more errors get detected by the oldest versions.
然后,最早的版本会检测到更多错误。
Now how to solve problem with sherlock action Bar menu item? 现在如何用夏洛克动作栏菜单项解决问题?
if you want it to work for android api 11 and higher(Android 4.x or 5.x ) simply add this piece of code: 如果您希望它适用于android api 11及更高版本(Android 4.x或5.x),只需添加以下代码:
Create a file in rsc/menu/ eg:menu.xml 在rsc / menu /中创建一个文件,例如:menu.xml
Now put: 现在放:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/send" android:title="@string/Send" android:icon="@drawable/ic_action_new_event" android:showAsAction="ifRoom" /> </menu>
And add a sample styles:in rsc/values/ : 并添加一个示例样式:在rsc / values /中:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" /> </resources>
This in your Activity: 这在您的活动中:
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.men, menu);
return true;
}
if you want it to work for android api 7 to 10(Android 2.x to Android 3.x) use something like this one in you class Activity: 如果您希望它适用于android api 7到10(android 2.x到android 3.x),请在类Activity中使用类似的内容:
THEME == R.style.Theme_Sherlock_Light; @Override public boolean onCreateOptionsMenu(Menu menu){ boolean isLight = THEME == R.style.Theme_Sherlock_Light; menu.add(R.string.Send) .setIcon(isLight ? R.drawable.ic_action_group : R.drawable.ic_action_person) .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); menu.add(R.id.get) .setIcon(isLight ? R.drawable.ic_action_chat: R.drawable.ic_action_chat) .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); menu.add("Refresh") .setIcon(isLight ? R.drawable.ic_action_time : R.drawable.ic_action_volume_on) .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); return true; }
To respond to a click use somthing like this: 要响应点击,请使用以下内容:
@Override public boolean onOptionsItemSelected(MenuItem item) { if (item.toString()==getString(R.string.Send)) { Toast.makeText(MainActivity.this, "Got click: " + item, Toast.LENGTH_SHORT).show(); } return super.onOptionsItemSelected(item); }
And add a sample styles:in src/values/ : 并在src / values /中添加示例样式:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme.Styled" parent="Theme.Sherlock.Light.DarkActionBar"> <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item> <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item> </style> <style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse"> <item name="background">@drawable/bg_striped</item> <item name="android:background">@drawable/bg_striped</item> <item name="backgroundSplit">@drawable/bg_striped_split</item> <item name="android:backgroundSplit">@drawable/bg_striped_split</item> </style> </resources>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.