android - 包裹在 CollapsingToolbarLayout 中时如何显示工具栏的 Logo 、图标、标题、副标题?

标签 android android-toolbar android-collapsingtoolbarlayout android-coordinatorlayout android-appbarlayout

Android Support Design Library发布后,想实现一个类似Twitter Profile页面的效果,其中Toolbartitlesubtitle 可以随着屏幕垂直滚动而改变。所以我尝试使用 CoordinatorLayoutAppBarLayoutCollapsingToolbarLayoutToolbar 来自 Android Support Design Library 来实现这个效果。除了 Toolbar 的内容无法按我的意愿显示或更改外,一切都按预期工作。我应该一直想显示 ToolbarcollapseIcon, navigationIcon, title, subtitle >,但即使我已经在布局中以编程方式设置了它们,它们也没有出现。正如 blog说,

Note that in those cases, you should call setTitle() on the CollapsingToolbarLayout, rather than on the Toolbar itself.

如果您检查了 doc ,你会发现CollapsingToolbarLayout只关注title的设置,而对collapseIconnavigationIcon副标题

那么有人能告诉我如何通过 ToolbarCollapsingToolbarLayout 实现这种效果吗?如果他们不能,那么还有什么其他的东西可以呢?有人对此有任何想法吗?

任何提示将不胜感激。提前致谢。

最佳答案

已报告此问题 here作为错误。我在我的一个应用程序中亲 body 验过这一点。为工具栏设置 topMargin 对我有用。正如有人在错误报告中评论的那样,这里是修复,作为问题报告的一个功能。

private void fixApi21ToolBarBug(Toolbar toolbar){
    if(Build.VERSION.SDK_INT!=21) return; //only on api 21
    final int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    final int result = (resourceId > 0) ? getResources().getDimensionPixelSize(resourceId) * 2 : 0;
    final CollapsingToolbarLayout.LayoutParams params = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
    params.topMargin -= result;
    toolbar.setLayoutParams(params);
}

在这里查看结果。

enter image description here

关于android - 包裹在 CollapsingToolbarLayout 中时如何显示工具栏的 Logo 、图标、标题、副标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31069107/

相关文章:

带有 MotionLayout 的 Android 折叠工具栏 - 当 RecyclerView 为空/不可滚动时禁用运动

java - 在 View 寻呼机中滚动时折叠工具栏不会折叠

android - 如何将所有操作项放在左侧,尽可能多地占用空间,同时在右侧溢出?

android - 折叠工具栏标题和主页/向上按钮之间的额外空间

android - 设置 CollapsingToolbarLayout 的起始高度

android - 抽屉导航汉堡图标只有在手动滑动抽屉后才会显示

java - 安卓|运行时获取 OkHTTP 库版本

android - requestLocationUpdates 与位置监听器

java - android md5加密与mysql或php不匹配?

API 19 上状态栏后面的 Android 工具栏