【问题标题】:Changing Toolbar and CollapsingToolbarLayout scroll flags programmatically以编程方式更改 Toolbar 和 CollapsingToolbarLayout 滚动标志
【发布时间】:2015-12-01 19:49:55
【问题描述】:

我有一个包含大量片段的 Activity android 应用。当我显示列表屏幕时,我想将Toolbarapp:layout_scrollFlags="scroll|enterAlways" 属性一起使用。在细节片段中,我想使用带有图像的CollapsingToolbarLayout。因为它是一个单一的 Activity 应用程序,所以我只有一个 Toolbar。是否可以通过编程方式修改我的布局以适应这两种情况?

【问题讨论】:

    标签: android android-layout android-fragments material-design android-support-design


    【解决方案1】:

    是的。假设您要从 CollapsingToolbarLayout 片段转到工具栏片段。

    1. 你使用AppBarLayout.setExpanded(false)折叠你的AppBarLayout

    2. 您可以根据需要更改滚动标志。

      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
      

    【讨论】:

    • 我也有这个问题。似乎我忘了调用setLayoutParams() 方法。感谢您的回答!
    • setScrollFlags(0) 工作正常,但是当我尝试将其设置为此时: params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP); // 清除所有滚动标志 toolbar.setLayoutParams(params);不工作..
    【解决方案2】:

    我发现它有效

    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);
    }
    

    【讨论】:

      【解决方案3】:

      为我工作。

      public void enableToolBarScrolling(CollapsingToolbarLayout toolbar) {
          AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
          params.setScrollFlags(SCROLL_FLAG_SCROLL | SCROLL_FLAG_ENTER_ALWAYS);
          toolbar.setLayoutParams(params);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-26
        • 1970-01-01
        相关资源
        最近更新 更多