在今天AppCompat更新出来之前,我可以改变Android L中按钮的颜色,但在旧版本上不行。在包含新的AppCompat更新后,我无法更改两个版本的颜色,当我尝试时,按钮就消失了。有人知道怎么改变按钮的颜色吗?
下面的图片展示了我想要达到的目标:
白色按钮是默认的,红色按钮是我想要的。
这是我之前在styles.xml中改变按钮颜色所做的:
<item name="android:colorButtonNormal">insert color here</item>
要动态地进行:
button.getBackground().setColorFilter(getResources().getColor(insert color here), PorterDuff.Mode.MULTIPLY);
我也改变了@android:style/ theme . material . light的主题父元素。到Theme.AppCompat.Light.DarkActionBar
更新
使用如下所示的设计支持库(23.2.0)和应用程序兼容性小部件
在Android支持库22.1中:
这是在膨胀布局-替换按钮时自动完成的
与appcompatextview, TextView与AppCompatTextView等确保
每个都可以支持着色。在这个版本中,那些色调意识
widget现在是公开可用的,允许您继续着色
支持,即使您需要子类化一个受支持的小部件。
色彩感知小部件的完整列表:
AppCompatAutoCompleteTextView
AppCompatButton
AppCompatCheckBox
AppCompatCheckedTextView
AppCompatEditText
AppCompatMultiAutoCompleteTextView
AppCompatRadioButton
AppCompatRatingBar
AppCompatSpinner
AppCompatTextView
前棒棒糖装置的材料设计:
AppCompat(又名ActionBarCompat)最初是作为
Android 4.0 ActionBar API用于运行在姜饼上的设备,
在反向移植实现的基础上提供一个公共API层
以及框架实现。AppCompat v21提供了一个API和
最新的Android 5.0功能集
我刚刚创建了一个android库,它允许您轻松地修改按钮颜色和波纹颜色
https://github.com/xgc1986/RippleButton
<com.xgc1986.ripplebutton.widget.RippleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Android button modified in layout"
android:textColor="@android:color/white"
app:buttonColor="@android:color/black"
app:rippleColor="@android:color/white"/>
你不需要为每个你想要不同颜色的按钮创建一个样式,允许你随机自定义颜色
布局:
<android.support.v7.widget.AppCompatButton
style="@style/MyButton"
...
/>
styles.xml:
<style name="MyButton" parent="Widget.AppCompat.Button.Colored">
<item name="backgroundTint">@color/button_background_selector</item>
<item name="android:textColor">@color/button_text_selector</item>
</style>
颜色/ button_background_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#555555"/>
<item android:color="#00ff00"/>
</selector>
颜色/ button_text_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#888888"/>
<item android:color="#ffffff"/>
</selector>