Ste*_*ane 87 android android-appcompat
在新的AppCompat库中,我们可以通过这种方式对按钮进行着色:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/follow"
android:id="@+id/button_follow"
android:backgroundTint="@color/blue_100"
/>
Run Code Online (Sandbox Code Playgroud)
如何在我的代码中以编程方式设置按钮的色调?我基本上试图根据一些用户输入实现按钮的条件着色.
Col*_*ire 125
根据文档相关的方法android:backgroundTint
是setBackgroundTintList(ColorStateList list)
更新
请按照此链接了解如何创建颜色状态列表资源.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="#your_color_here" />
</selector>
Run Code Online (Sandbox Code Playgroud)
然后使用它加载它
setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.your_xml_name));
Run Code Online (Sandbox Code Playgroud)
where contextInstance
是一个实例Context
使用AppCompart
btnTag.setSupportButtonTintList(ContextCompat.getColorStateList(Activity.this, R.color.colorPrimary));
Run Code Online (Sandbox Code Playgroud)
dim*_*suz 65
你可以用
button.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.id.blue_100)));
Run Code Online (Sandbox Code Playgroud)
但是我建议你使用昨天刚刚发布的支持库drawable tinting:
Drawable drawable = ...;
// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
drawable = DrawableCompat.wrap(drawable);
// We can now set a tint
DrawableCompat.setTint(drawable, Color.RED);
// ...or a tint list
DrawableCompat.setTintList(drawable, myColorStateList);
// ...and a different tint mode
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);
Run Code Online (Sandbox Code Playgroud)
tse*_*tse 47
看起来像视图有自己的色调管理机制,所以更好的将色调列表:
ViewCompat.setBackgroundTintList(
editText,
ColorStateList.valueOf(errorColor));
Run Code Online (Sandbox Code Playgroud)
Ami*_*ian 29
这是在 kotlin 中的操作方法:
view.background.setTint(ContextCompat.getColor(context, textColor))
Run Code Online (Sandbox Code Playgroud)
Sha*_*000 17
通过提供真实的代码情况正确扩展dimsuz的答案,请参阅以下代码段:
Drawable buttonDrawable = button.getBackground();
buttonDrawable = DrawableCompat.wrap(buttonDrawable);
//the color is a direct color int and not a color resource
DrawableCompat.setTint(buttonDrawable, Color.RED);
button.setBackground(buttonDrawable);
Run Code Online (Sandbox Code Playgroud)
此解决方案适用于将drawable用作按钮背景的场景.它也适用于前Lollipop设备.
Osa*_*awi 13
在 Java 中
myButton.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.id.white)));
Run Code Online (Sandbox Code Playgroud)
在科特林
myButton.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.id.white)))
Run Code Online (Sandbox Code Playgroud)
你可以使用类似的东西:
myButton.backgroundTintList = AppCompatResources.getColorStateList(context, R.color.primary_variant)
Run Code Online (Sandbox Code Playgroud)
小智 7
你尝试过这样的事吗?
button.setBackgroundTintList(getResources().getColorStateList(R.id.blue_100));
Run Code Online (Sandbox Code Playgroud)
请注意,getResources()仅适用于活动.但它也可以在每个上下文中调用.
这在材料设计库的新材料按钮中很容易处理,首先,添加依赖项:
implementation 'com.google.android.material:material:1.1.0-alpha07'
Run Code Online (Sandbox Code Playgroud)
然后在您的 XML 中,将其用于您的按钮:
<com.google.android.material.button.MaterialButton
android:id="@+id/accept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/i_accept"
android:textSize="18sp"
app:backgroundTint="@color/grayBackground_500" />
Run Code Online (Sandbox Code Playgroud)
当你想改变颜色时,这里是 Kotlin 中的代码,它没有被弃用,它可以在 Android 21 之前使用:
accept.backgroundTintList = ColorStateList.valueOf(ResourcesCompat.getColor(resources,
R.color.colorPrimary, theme))
Run Code Online (Sandbox Code Playgroud)
我有一个类似的问题。我希望基于颜色 (int) 值为视图着色复杂的可绘制背景。我通过使用代码成功了:
ColorStateList csl = new ColorStateList(new int[][]{{}}, new int[]{color});
textView.setBackgroundTintList(csl);
Run Code Online (Sandbox Code Playgroud)
其中 color 是表示所需颜色的 int 值。这表示简单的 xml ColorStateList:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="color here"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
我设法让我的工作的方式是使用CompoundButtonCompat.setButtonTintList(button, colour)
.
据我了解,无论 android 版本如何,这都有效。
你可以使用DrawableCompat例如
public static Drawable setTint(Drawable drawable, int color) {
final Drawable newDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(newDrawable, color);
return newDrawable;
}
Run Code Online (Sandbox Code Playgroud)
如果你使用Kotlin
and Material Design
,你可以MaterialButton
像这样改变你的颜色:
myButton.background.setTintList(ContextCompat.getColorStateList(context, R.color.myColor))
Run Code Online (Sandbox Code Playgroud)
您可以通过为您创建扩展函数来更好地改进它MaterialButton
,以使您的代码更具可读性,并且您的编码更加方便:
fun MaterialButton.changeColor(color: Int) {
this.background.setTintList(ContextCompat.getColorStateList(context, color))
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以像这样在任何地方使用您的函数:
myButton.changeColor(R.color.myColor)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
96425 次 |
最近记录: |