本文介绍了如何以编程方式添加按钮色调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在新的AppCompat库中,我们可以通过以下方式设置按钮的颜色:
In the new AppCompat library, we can tint the button this way:
<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"
/>
如何在我的代码中以编程方式设置按钮的色调?我基本上是在尝试根据某些用户输入对按钮进行有条件的着色.
How can I set the tint of the button programmatically in my code?I'm basically trying to implement a conditional coloring of the button based on some user input.
推荐答案
根据文档 android:backgroundTint
的相关方法是 setBackgroundTintList(ColorStateList列表)
更新
按照此链接知道如何创建颜色状态列表资源.
Follow this link to know how create a Color State List Resource.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="#your_color_here" />
</selector>
然后使用
setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.your_xml_name));
其中contextInstance
是Context
使用AppCompart
using AppCompart
btnTag.setSupportButtonTintList(ContextCompat.getColorStateList(Activity.this, R.color.colorPrimary));
这篇关于如何以编程方式添加按钮色调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!