在工具栏上显示后退箭头

本文详细介绍了在Android应用中,如何将Toolbar用作ActionBar并显示后退箭头,包括在AppCompatActivity中设置方法,以及如何处理点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Display Back Arrow on Toolbar

I'm migrating from ActionBar to Toolbar in my application. 我正在从应用程序中的ActionBar迁移到Toolbar But I don't know how to display and set click event on Back Arrow on Toolbar like I did on Actionbar . 但是我不知道如何像在Actionbar上那样显示和设置Toolbar上向后箭头的click事件。

在此处输入图片说明

With ActionBar , I call mActionbar.setDisplayHomeAsUpEnabled(true) . 使用ActionBar ,我调用mActionbar.setDisplayHomeAsUpEnabled(true) But there is no the similar method like this. 但是,没有类似的方法。

Has anyone ever faced this situation and somehow found a way to solve it? 有没有人遇到过这种情况,并以某种方式找到了解决方法?


#1楼

参考:https://stackoom.com/question/1npIY/在工具栏上显示后退箭头


#2楼

If you are using an ActionBarActivity then you can tell Android to use the Toolbar as the ActionBar like so: 如果您使用的是ActionBarActivity则可以告诉Android将Toolbar用作ActionBar如下所示:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

And then calls to 然后致电

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

will work. 将工作。 You can also use that in Fragments that are attached to ActionBarActivities you can use it like this: 您还可以在附加到ActionBarActivities Fragment中使用它,您可以像这样使用它:

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

If you are not using ActionBarActivities or if you want to get the back arrow on a Toolbar that's not set as your SupportActionBar then you can use the following: 如果您不使用ActionBarActivities或者想要在未设置为SupportActionBarToolbar上获得后退箭头,则可以使用以下命令:

mActionBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
mActionBar.setNavigationOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       //What to do on back clicked
   }
});

If you are using android.support.v7.widget.Toolbar , then you should add the following code to your AppCompatActivity : 如果您使用的是android.support.v7.widget.Toolbar ,则应将以下代码添加到AppCompatActivity

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

#3楼

If you were using AppCompatActivity and have gone down the path of not using it, because you wanted to not get the automatic ActionBar that it provides, because you want to separate out the Toolbar , because of your Material Design needs and CoordinatorLayout or AppBarLayout , then, consider this: 如果您使用 AppCompatActivity和已经下降不使用它的路径,因为你想没有得到自动ActionBar ,它提供了,因为你想分离出Toolbar ,因为你的材料设计的需要和CoordinatorLayoutAppBarLayout ,然后,请考虑以下几点:

You can still use the AppCompatActivity , you don't need to stop using it just so that you can use a <android.support.v7.widget.Toolbar> in your xml. 您仍然可以使用AppCompatActivity ,而不必停止使用它,以便可以在xml中使用<android.support.v7.widget.Toolbar> Just turn off the action bar style as follows: 只需按如下所示关闭操作栏样式即可

First, derive a style from one of the NoActionBar themes that you like in your styles.xml , I used Theme.AppCompat.Light.NoActionBar like so: 首先,从您在喜欢NoActionBar主题之一推导出风格styles.xml ,我用Theme.AppCompat.Light.NoActionBar像这样:

<style name="SuperCoolAppBarActivity" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/primary_dark</item>
    ...
    ...
</style>

In your App's manifest, choose the child style theme you just defined, like so: 在您的应用清单中,选择刚定义的子样式主题,如下所示:

    <activity
        android:name=".activity.YourSuperCoolActivity"
        android:label="@string/super_cool"
        android:theme="@style/SuperCoolAppBarActivity">
    </activity>

In your Activity Xml, if the toolbar is defined like so: 在Activity Xml中,如果工具栏的定义如下:

...
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        />
...

Then, and this is the important part, you set the support Action bar to the AppCompatActivity that you're extending, so that the toolbar in your xml, becomes the action bar. 然后,这是最重要的一部分,你的支持,操作栏设置为AppCompatActivity你正在扩大,所以,在你的XML工具栏, 变为行动起来吧。 I feel that this is a better way, because you can simply do the many things that ActionBar allows, like menus, automatic activity title, item selection handling, etc. without resorting to adding custom click handlers, etc. 我觉得这是一种更好的方法,因为您可以简单地执行ActionBar允许的许多操作,例如菜单,自动活动标题,项目选择处理等,而无需诉诸添加自定义点击处理程序等。

In your Activity's onCreate override, do the following: 在活动的onCreate覆盖中,执行以下操作:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_super_cool);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);
    //Your toolbar is now an action bar and you can use it like you always do, for example:
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} 

#4楼

you can use the tool bar setNavigationIcon method. 您可以使用工具栏的setNavigationIcon方法。 Android Doc Android文档

mToolBar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);

mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        handleOnBackPress();
    }
});

#5楼

I see a lot of answers but here is mine which is not mentioned before. 我看到了很多答案,但这是我之前没有提到的。 It works from API 8+. 它可以从API 8+开始工作。

public class DetailActivity extends AppCompatActivity

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

    // toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // add back arrow to toolbar
    if (getSupportActionBar() != null){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // handle arrow click here
    if (item.getItemId() == android.R.id.home) {
        finish(); // close this activity and return to preview activity (if there is any)
    }

    return super.onOptionsItemSelected(item);
}

#6楼

There are many ways to achieve that, here is my favorite: 有很多方法可以实现,这是我的最爱:

Layout: 布局:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:navigationIcon="?attr/homeAsUpIndicator" />

Activity: 活动:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // back button pressed
        }
    });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值