问题更改搜索视图的后背按钮图标颜色 - Android Studio

发布于 2025-01-21 22:35:05 字数 1076 浏览 2 评论 0原文

我正在尝试更改关闭按钮的颜色和搜索视图的背面按钮。我的关闭按钮的颜色按预期更改了,但是我遇到的问题要更改后按钮的颜色。我希望它们俩都是黑色的,但是后面按钮现在像白色一样被卡住。

我已经浏览了许多解决方案来为类似的答案,但是大多数正在对应用程序的工具栏进行更改。我创建了一个自定义菜单资源,并且没有一个工具栏可以更改样式。

我还尝试使用可绘制的使用sethomeAsupIndicator函数更改支持操作栏的图标,但这没有效果。

这是我使用的少量代码来更改近距离和背面的按钮颜色。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem menuItem = menu.findItem(R.id.navbar_search);
    SearchView searchView = (SearchView) menuItem.getActionView();
    
    ImageView closeImgView = searchView.findViewById(R.id.search_close_btn);
    closeImgView .setColorFilter(Color.BLACK);
    
    ImageView backImgView = searchView.findViewById(R.id.search_mag_icon);
    backImgView .setColorFilter(Color.BLACK);
    .
    .
    .
}

此返回按钮与r.id.search_mag_icon不一样吗?如果不是,那么这个后退按钮来自哪里,我该如何更改其样式?

I am trying to change the color of the close button and the back button of my SearchView. I have got the color of the close button changing as expected, but I am having issues getting the back button's color to change. I want them both to be black, but the back button is stuck as white right now.

enter image description here

I have looked through a bunch of solutions to similar answers, but most are making style changes to the app's Toolbar. I have created a custom menu resource and do not have a Toolbar to change the style of.

I have also tried changing the Drawable icon of the support action bar using the setHomeAsUpIndicator function, but this had no effect.

This is the small amount of code I am using to change the close and back button colors.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem menuItem = menu.findItem(R.id.navbar_search);
    SearchView searchView = (SearchView) menuItem.getActionView();
    
    ImageView closeImgView = searchView.findViewById(R.id.search_close_btn);
    closeImgView .setColorFilter(Color.BLACK);
    
    ImageView backImgView = searchView.findViewById(R.id.search_mag_icon);
    backImgView .setColorFilter(Color.BLACK);
    .
    .
    .
}

Is this back button not the same as the R.id.search_mag_icon? If not, then where is this back button coming from and how can I change its style?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深已缘浅 2025-01-28 22:35:05

您可以尝试以这样的XML设置imageView颜色

<ImageView
        android:id="@+id/search_mag_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="your image file"
        android:backgroundTint="@android:color/black"  /*for changing your image colour*/ 
 />

,也可以尝试使用SetbackgroundColour进行替代

search_mag_icon.setBackgroundColor(Color.parseColor("#00000"));

You can try for setting your imageview colour in xml like this

<ImageView
        android:id="@+id/search_mag_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="your image file"
        android:backgroundTint="@android:color/black"  /*for changing your image colour*/ 
 />

or try use setBackgroundColour for alternate

search_mag_icon.setBackgroundColor(Color.parseColor("#00000"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文