在新的 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"
/>
如何在代码中以编程方式设置按钮的色调?
我基本上试图根据一些用户输入实现按钮的条件着色。
根据文档相关方法android:backgroundTint
is setBackgroundTintList(ColorStateList 列表)
Update
按照这个link了解如何创建颜色状态列表资源。
<?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));
where contextInstance
是一个实例Context
使用AppCompart
btnTag.setSupportButtonTintList(ContextCompat.getColorStateList(Activity.this, R.color.colorPrimary));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)