安卓开发toolbar设置logo_如何显示工具栏的标志,图标,标题,副标题裹在CollapsingToolbarLayout什么时候?...

After Android Support Design Library was released, I wanted to implement an effect like a page of Twitter Profile, in which Toolbar's title and subtitle could be changed as screen scrolled vertically. So I tried to use CoordinatorLayout, AppBarLayout, CollapsingToolbarLayout and Toolbar from Android Support Design Library to achieve this effect. Everything worked as expected except that Toolbar's content couldn't be showed or changed as I wanted. I should have been wanting to display collapseIcon, navigationIcon, title, subtitle of Toolbar, but they didn't show up even though I had set them in the layout and programmatically. Just as the blog said,

Note that in those cases, you should call setTitle() on the

CollapsingToolbarLayout, rather than on the Toolbar itself.

If you have checked the doc, you would find out CollapsingToolbarLayout only focus on settings of title, but can do nothing about collapseIcon, navigationIcon and subtitle.

So could someone tell me how to achieve this effect by Toolbar and CollapsingToolbarLayout? If they couldn't be able, then what other things could be? Anybody has any ideas about this?

Any tips will be appreciated. Thanks in advance.

解决方案

This issue was reported here as a bug. I have personally experienced this with one of my apps. Setting the topMargin for the toolbar works for me. As somebody commented on the bug report, here is the fix, as a function from the issue report.

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

}

See the result here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值