Vin*_*edi 2 android android-menu
我不知道如何从菜单中捕获后退事件:
我的意思是我想在按button屏幕左上方的后退时捕获事件。
这是菜单的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/ActionModeMenu">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:id="@+id/context_menu_select_tweet_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:onClick="onSelectAllTweet" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:theme="@style/ActionModeMenuSubtitle"
android:text="@string/menu_tweet_select_all_text"/>
</LinearLayout>
<TextView
android:id="@+id/context_menu_select_tweet_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:theme="@style/ActionModeMenuTitle"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
谢谢 !
使用 android.R.id.home
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)
如果要更改后退箭头的图像,可以在OnCreate方法中编写此代码
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.mipmap.back_to); // change the image
Run Code Online (Sandbox Code Playgroud)