[英]How can I add this extra icon in toolbar for some fragments
I am making an android application which have a design like this:我正在制作一个具有如下设计的android应用程序:
https://imgur.com/R1JL7Na https://imgur.com/R1JL7Na
So my design idea is that I will make a single Activity
and all the screens will be Fragment
type and I will do Fragment Transaction
to change to screens or we can say fragments.所以我的设计理念是我将制作一个单独的
Activity
并且所有屏幕都是Fragment
类型,我将执行Fragment Transaction
以更改为屏幕,或者我们可以说Fragment Transaction
。 This will keep same toolbar across all fragments .这将在所有片段中保持相同的工具栏。 But some fragments, can say 4-5 fragments toolbar should have one extra icon.
但是有些片段,可以说4-5个片段工具栏应该多出一个图标。 Like in below image.
如下图所示。
https://imgur.com/a/LhP0irZ https://imgur.com/a/LhP0irZ
The extra icon is filters icon.额外的图标是过滤器图标。 So how can I add this extra icon in toolbar for some fragments?
那么如何在工具栏中为某些片段添加这个额外的图标呢? Do I have to make an extra
Activity
with different toolbar layout ?我是否必须使用不同的工具栏布局制作额外的
Activity
?
Please provide me some good way to achieve this请为我提供一些实现这一目标的好方法
Every Fragment
can has different menu.每个
Fragment
可以有不同的菜单。
In Fragment's onCreate
method add this:在 Fragment 的
onCreate
方法中添加:
setHasOptionsMenu(true);
and after that override onCreateOptionsMenu
callback in the Fragment
.然后覆盖
Fragment
onCreateOptionsMenu
回调。
In fragment在片段中
Intent intent = new Intent(context,Activity.class)
intent.putExtra("pin",pins);
System.out.println(pin);
In Activity活动中
pined=getIntent().getStringExtra("pin");
System.out.println(pined);
在活动类的工具栏中放置一个显示/隐藏第三个按钮的方法,并在fragments
访问您的activity
,并在每个fragment
使用setUserVisibleHint
,您将知道要在哪个fragment
上显示第三个按钮。
you can override onCreateOptionsMenu method in your class and override onOptionsItemSelected try您可以在类中覆盖 onCreateOptionsMenu 方法并覆盖 onOptionsItemSelected 尝试
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.demo,menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return super.onOptionsItemSelected(item)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.