简体   繁体   English

工具栏 - 添加向上按钮

[英]Toolbar - add the up button

I am trying to use the Toolbar instead of the ActionBar, but I can't figure out how to add the up button to return to the previous activity.我正在尝试使用工具栏而不是操作栏,但我不知道如何添加向上按钮以返回上一个活动。 I couldn't find any method that could relate to it.我找不到任何可能与之相关的方法。

How do I add the up button?如何添加向上按钮?

I guess what you are looking for is something like this:我想你正在寻找的是这样的:

Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar_detail);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Or in case of using in Fragment:或者在 Fragment 中使用的情况下:

Toolbar toolbar = (Toolbar) view.findViewById(R.id.app_bar_detail);
((ActionBarActivity) getActivity()).setSupportActionBar(toolbar);
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

This will show up your Action Bar inside of your toolbar, but don't worry everything will fit together well.这将在您的工具栏内显示您的操作栏,但不要担心一切都会很好地结合在一起。 The last you have to do if you dont want any shadow under your action bar or any background of it is change your theme in vaules/styles.xml.如果您不想在操作栏或它的任何背景下有任何阴影,那么您必须做的最后一件事情是在 vaules/styles.xml 中更改您的主题。

<style name="AppThmeme.Base" parent="Theme.AppCompat.NoActionBar">

If you want to do this in XML, you can use...如果要在 XML 中执行此操作,可以使用...

<android.support.v7.widget.Toolbar
            app:navigationIcon="?homeAsUpIndicator"
            ...

If you're wondering why clicking on up button doesn't work with fragments, you need to set up a navigation listener as well, not sure why Google hasn't enabled it by default:如果你想知道为什么点击向上按钮对片段不起作用,你还需要设置一个导航侦听器,不知道为什么谷歌默认没有启用它:

protected fun setupToolbar(toolbar: Toolbar) {
    (activity as AppCompatActivity).run {
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        toolbar.setNavigationOnClickListener { onBackPressed() }
    }
}

In case when previous activity is always same for an activity then up/back button can be achieved with the help of parentActivityName attribute.如果某个活动的先前活动始终相同,则可以在parentActivityName属性的帮助下实现向上/后退按钮。 It can be mentioned in AndroidManifest.xml file as shown below:它可以在AndroidManifest.xml文件中提及,如下所示:

<activity android:name=".DetailActivity"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>

Let's say DetailActivity was opened from MainActivity .假设DetailActivity是从MainActivity打开的。 So when you're on DetailActivity then tool bar will automatically show a left pointing arrow as shown in the screenshot below:因此,当您在DetailActivity工具栏将自动显示一个向左的箭头,如下面的屏幕截图所示:

在此处输入图片说明

When we click on left pointing arrow then MainActivity gets displayed.当我们单击向左箭头时, MainActivity就会显示出来。

Calling getSupportActionBar().setHomeButtonEnabled(true);调用getSupportActionBar().setHomeButtonEnabled(true); should still work I think, as long as you have already called setSupportActionBar(toolbar);我认为应该仍然有效,只要您已经调用了setSupportActionBar(toolbar);

You can add your own 'up' button in toolbar, after all it is just a ViewGroup.您可以在工具栏中添加自己的“向上”按钮,毕竟它只是一个 ViewGroup。

You can customize toolbar as much as you want, in your toolbar.xml, or wherever you have defined android.support.v7.widget.Toolbar in your layout add your 'up' button like given below :您可以根据需要自定义工具栏,在您的 toolbar.xml 中,或者您在布局中定义 android.support.v7.widget.Toolbar 的任何地方添加您的“向上”按钮,如下所示:

<android.support.v7.widget.Toolbar
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/toolbar"
   android:minHeight="?attr/actionBarSize"
   android:layout_height="?attr/actionBarSize"
   android:background="@drawable/color_toolbar"
   android:layout_width="match_parent">

   <ImageButton
       android:id="@+id/upButton"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:onClick="uphandler"
       android:src="@drawable/backbutton"
       android:layout_gravity="end"/>

</android.support.v7.widget.Toolbar>

Now, define uphandler function in your activity to listen to this up button :现在,在您的活动中定义 uphandler 函数来收听这个向上按钮:

public void uphandler(View v){
       this.finish();    // This will kill current activity, and if previous activity is still opened in background, it will come in front.
}

if you are using navigation component, then adding up button in fragment would be like this:如果您使用导航组件,那么在片段中添加按钮将是这样的:
in your fragment.xml在您的 fragment.xml 中

<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".presentation.recipeitem.RecipeDetailsFragment">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_constraintTop_toTopOf="parent"
        android:elevation="4dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />
.
.

then in your fragment you would do this然后在你的片段中你会这样做
inside onViewCreated(..)在 onViewCreated(..)

val navController = findNavController()
val appBarConfiguration = AppBarConfiguration(navController.graph)
viewBinding.toolbar.setupWithNavController(navController, appBarConfiguration)

if you need to add title then use this in onResume如果您需要添加标题,请在 onResume 中使用它

    viewBinding.toolbar.title = "my title"

You have to add these line back button show automatically您必须添加这些行后退按钮自动显示

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

on back, button click automatically back to activity在背面,按钮单击自动返回活动

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}

if you are using newer version of android studio:如果您使用的是较新版本的 android studio:

first declare toolbar of androidx not android首先声明androidx的工具栏不是android

 <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorAccent"
        app:popupTheme="@style/Theme.Sunshine.PopupOverlay" />

second, get a reference to the toolbar in Activity.java and use following code;其次,在 Activity.java 中获取对工具栏的引用并使用以下代码;

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar ab= getSupportActionBar();
    ab.setDisplayHomeAsUpEnabled(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM