我使用ActionBarSherlock。我有一个菜单项,我想使用自定义选择,只有该菜单项,而不是其他人的动作条。这是菜单code:
I'm using ActionBarSherlock. I have a MenuItem, and I want to use a custom selector with only that MenuItem, not the others in the ActionBar. This is the menu code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_include_location"
android:icon="@drawable/icon_place_selector"
android:showAsAction="always"
android:title="@string/menu_include_location"/>
<item
android:id="@+id/menu_send"
android:icon="@drawable/ic_send"
android:showAsAction="ifRoom|withText"
android:title="@string/menu_send"/>
</menu>
下面是icon_place_selector.xml
Here is the icon_place_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/location_place" android:state_pressed="false"/>
<item android:drawable="@drawable/location_no_gradient" android:state_pressed="true"/>
</selector>
问题是,在菜单项,图标只是其中的一小部分。以下是显示的内容截图。整个背景应该改变,而不仅是图标。我该怎么办呢?
The issue is that in the MenuItem, the icon is only a small part of it. Here's a screenshot of what shows up. The entire background should change, and not just the icon. How do I do that?
您可以通过在code设置自定义而ActionView更改特定动作栏项目的选择:
You can change the selector of a particular action bar item by setting a custom ActionView in the code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
MenuItem menuItem = menu.findItem(R.id.menu_include_location);
ImageView image = new ImageView(this);
image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
image.setPadding(16, 0, 16, 0);
image.setImageResource(R.drawable.ic_launcher);
image.setBackgroundResource(R.drawable.icon_place_selector);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// ...
}
});
menuItem.setActionView(image);
return true;
}
应用这些样式,如果你想改变选择的所有动作栏项目:
Apply these styles if you want to change the selector for all action bar items:
<style name="AppTheme" parent="@style/Theme.Sherlock">
<item name="android:selectableItemBackground">@drawable/icon_place_selector</item>
<item name="android:actionBarItemBackground">@drawable/icon_place_selector</item>
</style>
上一篇:KSOAP 2的Android使用HTTPSAndr、KSOAP、HTTPS、oid
下一篇:android.intent.action.SCREEN_ON不作为接收方的意图过滤器工作不作为、过滤器、意图、工作