android icon颜色转换工具,android – 如何以编程方式设置工具栏collapseIcon颜色

我想在显示搜索时更改工具栏中后退按钮的颜色(带圆圈的白色箭头).

3JuDJ.png

47497e85d6261c19e3ebd1867baae0e6.png

我设法改变了所有其他元素的颜色,我坚持使用后箭头颜色.

我可以从xml设置collapseIcon(后退箭头drawable):

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary"

app:layout_scrollFlags="enterAlways"

app:popupTheme="@style/AppTheme.PopupOverlay"

app:collapseIcon=I_WANT_TO_SET_THIS_PROGRAMMATICALLY>

我将app:collapseIcon设置为我想要的任何可绘制的,但是,我需要动态设置它.

我在这里找到的建议都不适合我.

不是这个:

final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable. abc_ic_ab_back_material);

upArrow.setColorFilter(myColor, PorterDuff.Mode.SRC_ATOP);

actionBar.setDisplayHomeAsUpEnabled(true);

actionBar.setHomeAsUpIndicator(upArrow);

或这个:

appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

@Override

public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

Drawable d = ContextCompat.getDrawable(MyActivity.this, R.drawable.ic_back_white);

d.setColorFilter(myColor, PorterDuff.Mode.SRC_ATOP);

toolbar.setNavigationIcon(d);

// Drawable d = ContextCompat.getDrawable(MyActivity.this, R.drawable.ic_back_white);

// d.setColorFilter(myColor, PorterDuff.Mode.SRC_ATOP);

// getSupportActionBar().setHomeAsUpIndicator(d);

}

});

我找不到任何其他东西.

有人可以帮忙吗?

谢谢

解决方法:

终于搞定了……

这对我有用:

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

AppBarLayout appBar = (AppBarLayout) findViewById(R.id.appbar);

appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

@Override

public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

for (int i = 0; i < toolbar.getChildCount(); i++) {

View view = toolbar.getChildAt(i);

if (view instanceof ImageButton) {

ImageButton btn = (ImageButton) view;

Drawable drawable = btn.getDrawable();

drawable.setColorFilter(new_button_color, PorterDuff.Mode.SRC_ATOP);

btn.setImageDrawable(drawable);

}

}

}

});

标签:android,android-actionbar,searchview,toolbar

来源: https://codeday.me/bug/20190627/1306870.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值