在Android Support Design Library发布后,想实现一个类似Twitter Profile页面的效果,其中Toolbar
的title
和 subtitle
可以随着屏幕垂直滚动而改变。所以我尝试使用 CoordinatorLayout
、AppBarLayout
、CollapsingToolbarLayout
和 Toolbar
来自 Android Support Design Library 来实现这个效果。除了 Toolbar
的内容无法按我的意愿显示或更改外,一切都按预期工作。我应该一直想显示 Toolbar
的 collapseIcon
, 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
的设置,而对collapseIcon
、navigationIcon
和副标题
。
那么有人能告诉我如何通过 Toolbar
和 CollapsingToolbarLayout
实现这种效果吗?如果他们不能,那么还有什么其他的东西可以呢?有人对此有任何想法吗?
任何提示将不胜感激。提前致谢。
最佳答案
已报告此问题 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);
}
在这里查看结果。
关于android - 包裹在 CollapsingToolbarLayout 中时如何显示工具栏的 Logo 、图标、标题、副标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31069107/