工具栏 - 添加了按钮工具栏、按钮

2023-09-07 16:59:00 作者:孤梦一生还有你

我想使用的工具栏,而不是动作条,但我无法弄清楚如何添加向上按钮返回到previous活动。我找不到,可能涉及到的任何方法。

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:

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

这将显示你的动作条工具栏里面,但不要担心,一切都会完全吻合。最后,你需要做的,如果你不想你的动作条下的任何阴影或它的任何背景是改变你的主题vaules / styles.xml。

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.

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