问题描述
在我的android应用程序中,我有一个活动和很多片段.但是,我只想显示某些片段的工具栏,而对于其他片段,我希望该片段为全屏显示.最好的推荐方法是什么(根据可见片段显示和隐藏活动工具栏)?
In my android application I have one activity and many fragments. However, I only want to show the toolbar for some fragments, for the others I want the fragment to be fullscreen. What's the best and recommended way to do this (show and hide the activity toolbar according to the visible fragment)?
推荐答案
我更喜欢为此使用接口.
I preferred using interface for this.
public interface ActionbarHost {
void showToolbar(boolean showToolbar);
}
使您的活动实现为ActionbarHost
,并将showToolbar替换为.
make your activity implement ActionbarHost
and override showToolbar as.
@Override
public void showToolbar(boolean showToolbar) {
if (getSupportActionBar() != null) {
if (showToolbar) {
getSupportActionBar().show();
} else {
getSupportActionBar().hide();
}
}
}
现在片段中的内容从onAttach()
private ActionbarHost actionbarHost;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof ActionbarHost) {
actionbarHost = (ActionbarHost) context;
}
}
现在,只要您想隐藏片段中的操作栏调用actionbarHost.showToolbar(false);
.
now just if you want to hide action bar call actionbarHost.showToolbar(false);
from fragment.
if (actionbarHost != null) {
actionbarHost.showToolbar(false);
}
我也建议在onDetach()
@Override
public void onDetach() {
super.onDetach();
if (actionbarHost != null) {
actionbarHost.showToolbar(true);
}
}
这篇关于根据可见片段处理活动工具栏可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!