[英]Android TextView DrawableTint on pre v23 devices
Is there any way we can tint the Drawable
used in the TextView
?有什么方法可以为
TextView
使用的Drawable
着色? DrawableTint
works only on API level 23 and above. DrawableTint
仅适用于 API 级别 23 及更高级别。
Currently I'm using a Vertical Linear Layout
to achieve my requirement.目前我正在使用
Vertical Linear Layout
来实现我的要求。
<LinearLayout style="@style/ChoiceIllustratorIconTextContainerStyle">
<ImageView
style="@style/ChoiceIllustratorImageStyle"
android:contentDescription="@string/cd_university"
android:src="@drawable/ic_account_balance_white_24dp" />
<TextView
style="@style/ChoiceIllustratorTextStyle"
android:text="@string/ci_text_university" />
</LinearLayout>
Android studio is suggesting me to use Compound Drawble
with TextView
to achieve this. Android studio 建议我使用
Compound Drawble
和TextView
来实现这一点。 And I'm able to achieve it, but I cannot find a way to Tint
the drawable.我能够实现它,但我找不到一种方法来为可绘制对象
Tint
。
<TextView
style="@style/ChoiceIllustratorTextStyle"
android:drawablePadding="4dp"
android:drawableTop="@drawable/ic_account_balance_white_24dp"
android:text="@string/ci_text_university" />
AndroidX appcompat library supports tinting in TextView since version 1.1.0-alpha03 [ ref ] . AndroidX appcompat 库从 1.1.0-alpha03 [ ref ]版本开始支持 TextView 中的着色。
Add dependencies to appcompat library向 appcompat 库添加依赖项
dependencies {
implementation "androidx.appcompat:appcompat:1.1.0"
}
Then drawable in TextView can be tinted from XML like this然后 TextView 中的 drawable 可以像这样从 XML 着色
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:drawableStartCompat="@drawable/ic_plus"
app:drawableTint="@color/red" />
Don't forget to include不要忘记包括
xmlns:app="http://schemas.android.com/apk/res-auto"
and to extend your Activity from AppCompatActivity
.并从
AppCompatActivity
扩展您的 Activity 。
The programmatic way to do this is执行此操作的编程方式是
Drawable[] drawables = textView.getCompoundDrawables();
if (drawables[0] != null) { // left drawable
drawables[0].setColorFilter(color, Mode.MULTIPLY);
}
This works for all API levels.这适用于所有 API 级别。
This is your best option for pre-Marshmallow devices.这是棉花糖前设备的最佳选择。
This answer is based on @kris larson suggestion.这个答案基于@kris larson 的建议。
I use the following methods and it is working fine on all devices.我使用以下方法,它在所有设备上都能正常工作。
setTintedCompoundDrawable
a custom method that takes the TextView
to which you would want to set the compound drawable, a drawable res id & and the res id of your color choice. setTintedCompoundDrawable
一个自定义方法,它采用您想要设置复合可绘制的TextView
、可绘制的 res id & 和您选择的颜色的 res id。
private void setTintedCompoundDrawable(TextView textView, int drawableRes, int tintRes) {
textView.setCompoundDrawablesWithIntrinsicBounds(
null, // Left
Utils.tintDrawable(ContextCompat.getDrawable(getContext(), drawableRes),
ContextCompat.getColor(getContext(), tintRes)), // Top
null, // Right
null); //Bottom
// if you need any space between the icon and text.
textView.setCompoundDrawablePadding(12);
}
Tint tintDrawable
method goes like this: Tint
tintDrawable
方法是这样的:
public static Drawable tintDrawable(Drawable drawable, int tint) {
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, tint);
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_ATOP);
return drawable;
}
您可以在这种情况下使用TextViewCompat
类:
TextViewCompat.setCompoundDrawableTintList(TextView, ColorStateList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.