如何向操作栏/工具栏添加后退按钮
How to add a back button to action bar / toolbar
我写了一段代码作为应用程序的一部分,我想在操作 bar/tool 栏上实现一个后退按钮,这样当按下按钮时,上一页(page/fragment 将显示在当前 page/fragment) 之前。
这是 ToolBar、DrawerLayout、NavigationView 和 getSupportActionBar() 的代码:
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setVisibility(View.VISIBLE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
我无法使用 ActionBar。出于某种原因(我不知道为什么),我的 Android 工作室/程序不允许我使用 ActionBar。所以我将其替换为 set/getSupportActionBar().
与此相关的函数是:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
我的 activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/activity_main"
android:orientation="vertical"
tools:openDrawer="start"
tools:context="com.example.albin.settings_menu.SettingsActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Settings"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar">
</FrameLayout>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:layout_marginTop="-24dp"
app:menu="@menu/options_menu" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
</LinearLayout>
问题是我不知道哪些是有用的代码,哪些是无用的代码以及如何mix/join/(添加额外的代码)这些(代码,方法,variables/objects、片段、xml 布局)以获得所需的结果,即在操作 bar/tool 栏上应用后退按钮。
上面的大部分代码是针对向上按钮而不是后退按钮实现的。我在几个地方读到向上和向后按钮不一样。
我尝试了 Internet 和本网站上的几个链接,但其中 none 个正是我所需要的。
希望有人能给我一个明确的答案...
最简单的方法是按照开发人员文档的建议在清单文件中添加父项 activity。
<activity
android:name=".ChildActivity"
android:parentActivityName=".ParentActivity" >
和java代码你已经完成了,setSupportActionbar和setHomeAsUpEnabled。
已编辑:
有必要为图标添加可见的操作,如中所述
Android Developer Docs
因此,工具栏为修改 Android 中的 title-bar 提供了更大的灵活性。
至于为什么 getActionBar
不起作用而您被迫使用 getSupportActionBar
是因为您必须使用 SupportLibrary
。 SupportLibrary 向后兼容早期的 SDK 版本。
If you want to modify your title-bar/header/action-bar extensively
then use toolbar otherwise use action-bar.
向您的工具栏添加导航点击侦听器,如下所示
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
实际上你的布局有这个问题,因为你在 RelativeLayout
中添加了工具栏,所以抽屉布局重叠在上面,这就是你无法点击后退箭头的原因,我已经修复了你的布局,见下文
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:navigationIcon="@drawable/ic_back_black"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Settings" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/options_menu" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
您可以在 ToolBar
中包含后退图标:
初始化ToolBar
:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
您可以使用 drawable
图标作为后退按钮。
toolbar.setNavigationIcon(R.drawable.your_drawable_icon);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// what do you want here
}
});
如果您不想使用 drawable
图标,那么:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// what do you want here
}
});
如果您从操作栏引用某些操作,例如保存操作或共享操作,并且您正在覆盖 onOptionsItemSelected 方法,那么您需要定义行为单击 返回 或 主页 按钮时:
@Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save:
//save stuff
break;
//this is what you need to add to reference again back/home button
case android.R.id.home:
//do your stuff here, usually back to the home or close the current activity
getActivity().finish();
break;
default:
break;
}
return true;
我写了一段代码作为应用程序的一部分,我想在操作 bar/tool 栏上实现一个后退按钮,这样当按下按钮时,上一页(page/fragment 将显示在当前 page/fragment) 之前。
这是 ToolBar、DrawerLayout、NavigationView 和 getSupportActionBar() 的代码:
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setVisibility(View.VISIBLE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
我无法使用 ActionBar。出于某种原因(我不知道为什么),我的 Android 工作室/程序不允许我使用 ActionBar。所以我将其替换为 set/getSupportActionBar().
与此相关的函数是:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
我的 activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/activity_main"
android:orientation="vertical"
tools:openDrawer="start"
tools:context="com.example.albin.settings_menu.SettingsActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Settings"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar">
</FrameLayout>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:layout_marginTop="-24dp"
app:menu="@menu/options_menu" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
</LinearLayout>
问题是我不知道哪些是有用的代码,哪些是无用的代码以及如何mix/join/(添加额外的代码)这些(代码,方法,variables/objects、片段、xml 布局)以获得所需的结果,即在操作 bar/tool 栏上应用后退按钮。
上面的大部分代码是针对向上按钮而不是后退按钮实现的。我在几个地方读到向上和向后按钮不一样。
我尝试了 Internet 和本网站上的几个链接,但其中 none 个正是我所需要的。
希望有人能给我一个明确的答案...
最简单的方法是按照开发人员文档的建议在清单文件中添加父项 activity。
<activity
android:name=".ChildActivity"
android:parentActivityName=".ParentActivity" >
和java代码你已经完成了,setSupportActionbar和setHomeAsUpEnabled。
已编辑: 有必要为图标添加可见的操作,如中所述 Android Developer Docs
因此,工具栏为修改 Android 中的 title-bar 提供了更大的灵活性。
至于为什么 getActionBar
不起作用而您被迫使用 getSupportActionBar
是因为您必须使用 SupportLibrary
。 SupportLibrary 向后兼容早期的 SDK 版本。
If you want to modify your title-bar/header/action-bar extensively then use toolbar otherwise use action-bar.
向您的工具栏添加导航点击侦听器,如下所示
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
实际上你的布局有这个问题,因为你在 RelativeLayout
中添加了工具栏,所以抽屉布局重叠在上面,这就是你无法点击后退箭头的原因,我已经修复了你的布局,见下文
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:navigationIcon="@drawable/ic_back_black"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Settings" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/options_menu" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
您可以在 ToolBar
中包含后退图标:
初始化ToolBar
:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
您可以使用
drawable
图标作为后退按钮。toolbar.setNavigationIcon(R.drawable.your_drawable_icon); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // what do you want here } });
如果您不想使用
drawable
图标,那么:getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // what do you want here } });
如果您从操作栏引用某些操作,例如保存操作或共享操作,并且您正在覆盖 onOptionsItemSelected 方法,那么您需要定义行为单击 返回 或 主页 按钮时:
@Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save:
//save stuff
break;
//this is what you need to add to reference again back/home button
case android.R.id.home:
//do your stuff here, usually back to the home or close the current activity
getActivity().finish();
break;
default:
break;
}
return true;