以编程方式更改工具栏和CollapsingToolbarLayout滚动标志 [英] Changing Toolbar and CollapsingToolbarLayout scroll flags programmatically

查看:82
本文介绍了以编程方式更改工具栏和CollapsingToolbarLayout滚动标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有很多片段的Activity Android应用程序.当我显示列表屏幕时,我想将Toolbarapp:layout_scrollFlags="scroll|enterAlways"属性一起使用.在细节片段中,我想将CollapsingToolbarLayout与其中的图像一起使用.由于它是一个Activity应用程序,因此我只有一个Toolbar.是否可以通过编程方式修改我的布局以适合两种情况?

I have a single Activity android app with lots of fragments. When I'm showing a list screen I want to use the Toolbar with the, app:layout_scrollFlags="scroll|enterAlways" property. And in the detail fragments I want to use the CollapsingToolbarLayout with an image in it. Since it's a single Activity app, I have only one Toolbar. Is it possible to modify my layout programmatically to suit both cases?

推荐答案

是.假设您要从CollapsingToolbarLayout片段转到工具栏一个.

Yes. Let's say you are going from the CollapsingToolbarLayout fragment to the Toolbar one.

  1. 使用AppBarLayout.setExpanded(false)折叠AppBarLayout;

您可以更改滚动标志以适合您的需求.

You change the scroll flags to fit your needs.

AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
p.setScrollFlags(...);
toolbar.setLayoutParams(p);

如有必要,CollapsingToolbarLayout也是如此.我想应该是这样的:

Same goes for the CollapsingToolbarLayout if necessary. I guess it should be something like:

collapsingToolbarParams.setScrollFlags(0); //no flags for ctl
toolbarParams.setScrollFlags(SCROLL_FLAG_SCROLL | SCROLL_FLAG_ENTER_ALWAYS); //new flags for toolbar

这篇关于以编程方式更改工具栏和CollapsingToolbarLayout滚动标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆