如何在v7工具栏上使用工具栏的主页按钮提供向上导航 [英] How provide Up navigation with toolbar's home button on v7 toolbar

查看:108
本文介绍了如何在v7工具栏上使用工具栏的主页按钮提供向上导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动(import android.support.v7.widget.Toolbar;)中有一个工具栏,我正尝试使用其主页按钮提供Up导航.我所拥有的:

I have a toolbar in my activity ( import android.support.v7.widget.Toolbar; ) and I'm trying to provide Up navigation using its home button. What I have:

清单:

<!-- ... -->
<activity android:name=".SettingsActivity"
          android:label="@string/settings"
          android:parentActivityName=".MainActivity"/>
<!-- ... -->

view_toolbar.xml:

view_toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp">
</android.support.v7.widget.Toolbar>

activity_settings.xml:

activity_settings.xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Toolbar -->
    <include
        layout="@layout/view_toolbar" />

    <!-- ... -->

我的onCreate方法:

my onCreate method:

super.onCreate(bundle)
setContentView(R.layout.activity_settings);

// Set the toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

到目前为止,我不应该具有向上按钮,而我没有.所以我们很好.但是当我尝试添加它时,我做不到.

So far I shouldn't have an up button and I don't. So we're fine. But when I tried to add it, I couldn't.

首先,我尝试了这个:

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

没用. 然后,我尝试了此操作(如

Didn't work. Then I tried this (as shown here):

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    Toast.makeText(ToolbarActivity.this, "Up clicked", 
        Toast.LENGTH_SHORT).show();
    NavUtils.navigateUpFromSameTask(ToolbarActivity.this);
}
});

我什至尝试了一种在某处看到的解决方法,包括创建一个虚拟菜单并尝试从onOptionsItemSelected获取事件(该方式从未被调用).

I even tried a workaround I saw somewhere, involving creating a dummy menu and trying to get the event from onOptionsItemSelected (which is never called by the way).

我该怎么办?通过工具栏提供向上导航的正确方法是什么?

What can I do? What is the correct way to provide Up navigation through toolbar?

推荐答案

这很简单.只需执行以下步骤:

It's very simple. Just do the following steps:

  1. 将工具栏添加到activity_child.xml:

(在我的项目中,此AppBarLayout放置在内)

(In my project, this AppBarLayout is placed inside a ConstraintLayout)

<android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

...

  1. ChildActivity.java
  2. 中设置工具栏
  1. Set up the toolbar in the ChildActivity.java

public class ChildActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  }

  ...
}

  1. AndroidManifest.xml
  2. 中设置parentActivityName属性

<activity android:name=".ChildActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:parentActivityName=".ParentActivity"/>

重要

为使其正常工作,请在致电startActivity(intent)不要在父活动中致电finish().

For this to work properly, do not call finish() in the parent activity after you call startActivity(intent).

这篇关于如何在v7工具栏上使用工具栏的主页按钮提供向上导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆