2015-11-27 136 views

回答

73

如果您的工具栏位于可能位于CoordinatorLayout内的AppBarLayout中,那么应该这样工作。

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,这里是一些documentation参考,关键要注意的是“与AppBarLayout的滚动一样,此方法依赖于此布局是CoordinatorLayout的一个直接子节点。“希望这有助于!

+0

)惊人的回答,所有的工作,谢谢!!! – Artem

7

这是你在找什么?

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 

链接:How to enable/disable toolbar scrolling programmatically when using design support library

为了隐藏工具栏你可以做这样的事情:

toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start(); 

如果你想再次显示它您拨打:

toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start(); 
+1

@ johnarao07嗨!感谢您的回答,但它不适合我。此解决方案禁用隐藏/显示工具栏。我只想显示/隐藏没有禁用 – Artem

+0

喜兄弟!更新了答案! – johnrao07

+1

@ johnarao07 aa,不起作用(( – Artem

0

我的问题与@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答案): - programmatically-show-toolbar-after-hidden-by-scrolling-android-design-library

相关问题