gpt4 book ai didi

android - 提供的Android SDK图标可以设置颜色吗?

转载 作者:行者123 更新时间:2023-11-29 15:41:13 31 4
gpt4 key购买 nike

Android SDK 提供了以下图标。

有没有办法为这些设置颜色......如果可能的话,如何设置?

    <ImageView
android:id="@+id/share_button"
style="@style/IconNav"
android:src="@android:drawable/ic_menu_share"/>

<ImageView
android:id="@+id/bookmark_button"
style="@style/IconNav"
android:src="@android:drawable/ic_input_get"/>

enter image description here

更新

在完全刷新项目后,发现 Xml 中的 tint 属性起到了作用。

For the short answer

.. this is the solution that worked for me - adding the property to the ImageView xml:

    android:tint="@color/grass_dark"

@goldenb 的回答是对解决此问题的不同方法的全面介绍,因此我将其标记为答案。

最佳答案

您确实可以使用色调作为更改 ImageView 颜色的一种方式,但是您应该被提醒 android:tint 将始终应用在原始颜色之上。

如博主所述danlew

  • ImageView's tint mixes the tint color with the original asset. What you want is for the tint color to take over entirely; instead it applies the tint on top of the existing color. So, for example, if the source asset is black, and you want it to be #77FFFFFF (a translucent shade of white), you'll actually end up getting that shade of white with a black background beneath it.

  • android:tint is limited to ImageView. You want to be able to tint any Drawable in any View.

一种可能的替代方法是使用 android ColorFilter

根据official documentation :

A color filter can be used with a Paint to modify the color of each pixel drawn with that paint. This is an abstract class that should never be used directly.

您可以使用 ColorFilter 做很多或多或少复杂的事情,但是您如何应用它呢?

一个简单的example来自另一个这样的问题是:

//White tint
imageView.setColorFilter(Color.argb(255, 255, 255, 255));

or

imageView.setColorFilter(ContextCompat.getColor(context,R.color.COLOR_YOUR_COLOR))

或来自 here 的 SO 中的更完整答案

ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);



// we can create the color values in different ways:
redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );

如果你想了解更多,你应该检查这些链接

SO - What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?

setColorFilter()

Fast Android asset theming with ColorFilter

SO-Modifying the color of an android drawable

关于android - 提供的Android SDK图标可以设置颜色吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40076388/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com