Java 将工具栏设置为片段中的操作栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27030183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set up toolbar as actionbar in fragment
提问by Laurenswuyts
I want to set up my toolbar as an actionbar, but since your toolbar is a layoutelement it has to be in your layout. Now my layout is in my fragment.
我想将我的工具栏设置为操作栏,但由于您的工具栏是布局元素,因此它必须在您的布局中。现在我的布局在我的片段中。
I added the toolbar in my layout and I call it within my fragment:
我在布局中添加了工具栏,并在片段中调用它:
//Toolbar
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
It works because I can set the title and so on but now I want it to react as a actionbar because I want to have this actually. setDisplayHomeAsUpEnabled(true)
它有效,因为我可以设置标题等,但现在我希望它作为操作栏做出反应,因为我真的想要这个。setDisplayHomeAsUpEnabled(true)
To do that I have to change the toolbar to an actionbar:
为此,我必须将工具栏更改为操作栏:
setSupportActionBar(toolbar);
That doesn't work in my fragment ...
这在我的片段中不起作用......
Can anybody help me to get my toolbar to work as an actionbar in a fragment.
任何人都可以帮助我让我的工具栏作为片段中的操作栏工作。
采纳答案by Pratik Butani
Now ActionBarActivity
is deprecated so
You need to cast your activity from getActivity()
to AppCompatActivity
first. Here's an example:
现在ActionBarActivity
已弃用,因此您需要将您的活动从getActivity()
到AppCompatActivity
第一个。下面是一个例子:
((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle();
The reason you have to cast it is because getActivity()
returns a FragmentActivity
and you need an AppCompatActivity
您必须投射它的原因是因为getActivity()
返回 aFragmentActivity
并且您需要一个AppCompatActivity
回答by dominicoder
ActionBar is an Activity property. If you want to set a toolbar from a given fragment as the ActionBar of the owning Activity, then get the Activity that owns the fragment (Fragment.getActivity()
) and set its ActionBar property.
ActionBar 是一个 Activity 属性。如果要将给定片段的工具栏设置为所属 Activity 的 ActionBar,则获取拥有该片段的 Activity ( Fragment.getActivity()
) 并设置其 ActionBar 属性。
Then juse use the same setDisplayHomeAsUpEnabled method you mentioned to begin with on the ActionBar after setting your toolbar as the ActionBar to get the back / up button.
然后,在将工具栏设置为 ActionBar 后,使用您提到的相同 setDisplayHomeAsUpEnabled 方法在 ActionBar 上开始以获取后退/向上按钮。
You will obviously have to manage this carefully if your app has multiple fragments within that Activity.
如果您的应用程序在该 Activity 中有多个片段,您显然必须仔细管理这一点。
回答by Ravi
Use
用
((ActionBarActivity) getActivity()).getSupportActionBar().setSubtitle("Your Title");
回答by notdrone
try:
尝试:
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
回答by Miravzal
If you use Kotlin, try this:
如果你使用 Kotlin,试试这个:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, state: Bundle?): View? {
(activity as AppCompatActivity).setSupportActionBar(your_toolbar)
setHasOptionsMenu(true)
return inflater.inflate(R.layout.your_layout, container, false)
}
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
inflater?.inflate(R.menu.your_menu, menu)
}
回答by Micheal Mavericks
Lets say the Activity holding the fragment is MainActivity.
假设持有片段的 Activity 是 MainActivity。
Do
做
MainActivity main = (MainActivity)getActivity();
//You can access all public variable and methods of MainActivity.
//simply call
main.setSupportActionBar(toolbar)
main.getSupportActionBar.setTitle("title");