AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBar);
appBarLayout.setExpanded(true, true);
或者折叠它
AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBar);
appBarLayout.setExpanded(false, true);
这里是定义
setExpanded(boolean expanded, boolean animate)
请注意,此方法从支持库v23起可用,这里有一些文档可供参考,需要注意的关键事项是:“与AppBarLayout的滚动一样,此方法依赖于该布局直接作为CoordinatorLayout的子级。”
这就是您正在寻找的东西吗?
Toolbar toolbar = findViewById(R.id.toolbar); // or however you need to do it for your code
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0); // clear all scroll flags
为了隐藏工具栏,你可以像这样做:
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
如果你想再次显示它,可以调用:
toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
我的问题和@Artem非常相似,我尝试了许多修复方法,但都没有用。当您使用AppBarLayout
时,@Jraco11的答案是正确的。对我而言,@johnrao07不起作用。但是,当我们使用Toolbar
时,我找到了一个完美的解决方案。
通过编程隐藏工具栏
if (toolbar.getParent() instanceof AppBarLayout){
((AppBarLayout)toolbar.getParent()).setExpanded(false,true);
}
以编程方式显示工具栏
if (toolbar.getParent() instanceof AppBarLayout){
((AppBarLayout)toolbar.getParent()).setExpanded(true,true);
参考原回答(由@Android HHT回答):在 Android Design 库中,滚动时如何以编程方式显示被隐藏的工具栏