2015-11-27 64 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

相關問題