[英]Hide/Show Toolbar programmatically on CoordinatorLayout
如果您的工具栏位于AppBarLayout内,该AppBarLayout可能位于您的CoordinatorLayout内,那么这样的东西应该可行。
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回答): - 以 编程方式显示工具栏 - 隐藏 - 滚动 - 安卓 - 设计 - 库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.