在我的Android应用程序中,当我按下操作栏菜单时,它会发出蓝光。我该如何防止它发生?
是否可以只对一个菜单执行此操作并让其他菜单继续运行?
<style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:background">@drawable/action_bar_background</item>
<item name="android:selectableItemBackground">@android:color/transparent</item>
</style>
答案 0 :(得分:4)
您可以通过在代码中设置自定义ActionView来更改特定操作栏项的选择器:
@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.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;
}
您可以在icon_place_selector.xml中使用透明颜色作为按下状态,
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed -->
<item android:state_pressed="true" android:drawable="@android:color/transparent" />
</selector>
答案 1 :(得分:2)
您还可以通过actionLayout属性为每个菜单项创建布局,如下所示
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/search_options"
android:actionLayout="@layout/search_layout"
android:icon="@drawable/app_bar_icon_search"
android:showAsAction="ifRoom|collapseActionView"
android:title="@string/menu_home"
android:visible="false"/>
<item
android:id="@+id/current_folder_options"
android:showAsAction="ifRoom"
android:title="@string/menu_folder_options"
android:actionLayout="@layout/menu_folder_options_layout" />
<item
android:id="@+id/menu_slide_out"
android:actionLayout="@layout/ab_insigh_icon"
android:showAsAction="always"
android:title="@string/menu_slide"/>
</menu>
我的菜单项 * menu_folder_options_layout.xml * 之一的操作布局是,我在哪里使用选择器
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageButton
android:id="@+id/menu_option_image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/app_bar_icon_folder_options"
android:background="@drawable/appbar_press_selector" />
</RelativeLayout>