以编程方式更改工具栏和 CollapsingToolbarLayout 滚动标志
Changing Toolbar and CollapsingToolbarLayout scroll flags programmatically
我有一个 Activity android 应用程序,其中包含很多片段。当我显示列表屏幕时,我想将 Toolbar
与 app:layout_scrollFlags="scroll|enterAlways"
属性 一起使用。在细节片段中,我想使用 CollapsingToolbarLayout
和其中的图像。因为它是一个 Activity 应用程序,所以我只有一个 Toolbar
。是否可以通过编程方式修改我的布局以适应这两种情况?
是的。假设您要从 CollapsingToolbarLayout 片段转到工具栏片段。
您使用 AppBarLayout.setExpanded(false)
;
折叠了您的 AppBarLayout
您可以更改滚动标志以满足您的需要。
AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
p.setScrollFlags(...);
toolbar.setLayoutParams(p);
如有必要,同样适用于 CollapsingToolbarLayout。我想应该是这样的:
collapsingToolbarParams.setScrollFlags(0); //no flags for ctl
toolbarParams.setScrollFlags(SCROLL_FLAG_SCROLL | SCROLL_FLAG_ENTER_ALWAYS); //new flags for toolbar
I found it working
public void disableToolBarScrolling() {
CollapsingToolbarLayout toolbar = findViewById(R.id.collap_toolbar);
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0);
}
public void enableToolBarScrolling() {
CollapsingToolbarLayout toolbar = findViewById(R.id.collap_toolbar);
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(SCROLL_FLAG_SCROLL | SCROLL_FLAG_ENTER_ALWAYS);
}
适合我。
public void enableToolBarScrolling(CollapsingToolbarLayout toolbar) {
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(SCROLL_FLAG_SCROLL | SCROLL_FLAG_ENTER_ALWAYS);
toolbar.setLayoutParams(params);
}
我有一个 Activity android 应用程序,其中包含很多片段。当我显示列表屏幕时,我想将 Toolbar
与 app:layout_scrollFlags="scroll|enterAlways"
属性 一起使用。在细节片段中,我想使用 CollapsingToolbarLayout
和其中的图像。因为它是一个 Activity 应用程序,所以我只有一个 Toolbar
。是否可以通过编程方式修改我的布局以适应这两种情况?
是的。假设您要从 CollapsingToolbarLayout 片段转到工具栏片段。
您使用
AppBarLayout.setExpanded(false)
; 折叠了您的 您可以更改滚动标志以满足您的需要。
AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) toolbar.getLayoutParams(); p.setScrollFlags(...); toolbar.setLayoutParams(p);
如有必要,同样适用于 CollapsingToolbarLayout。我想应该是这样的:
collapsingToolbarParams.setScrollFlags(0); //no flags for ctl toolbarParams.setScrollFlags(SCROLL_FLAG_SCROLL | SCROLL_FLAG_ENTER_ALWAYS); //new flags for toolbar
AppBarLayout
I found it working
public void disableToolBarScrolling() {
CollapsingToolbarLayout toolbar = findViewById(R.id.collap_toolbar);
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0);
}
public void enableToolBarScrolling() {
CollapsingToolbarLayout toolbar = findViewById(R.id.collap_toolbar);
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(SCROLL_FLAG_SCROLL | SCROLL_FLAG_ENTER_ALWAYS);
}
适合我。
public void enableToolBarScrolling(CollapsingToolbarLayout toolbar) {
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(SCROLL_FLAG_SCROLL | SCROLL_FLAG_ENTER_ALWAYS);
toolbar.setLayoutParams(params);
}