如何使用全息主题更改Android中按钮的背景色?
在开发Android应用程序时,我遇到了这个问题。在使用安卓3.0全息主题时,我无法更改按钮的样式 在my theme.xml文件中是这样的:如何使用全息主题更改Android中按钮的背景色?,android,themes,android-3.0-honeycomb,Android,Themes,Android 3.0 Honeycomb,在开发Android应用程序时,我遇到了这个问题。在使用安卓3.0全息主题时,我无法更改按钮的样式 在my theme.xml文件中是这样的: <resources> <style name="Test" parent="android:Theme.Holo"> </style> </resources> 我的manifest.xml文件包含以下内容: <application android:icon="@dr
<resources>
<style name="Test" parent="android:Theme.Holo">
</style>
</resources>
我的manifest.xml文件包含以下内容:
<application android:icon="@drawable/my_icon"
android:label="@string/app_name" android:debuggable="false" android:theme="@style/Test">
如果我在此时运行应用程序,我会得到默认的全息主题按钮。但是我想改变它们的颜色。这似乎是一个透明度问题,但我不确定该去哪里寻找
我尝试过创建自定义按钮样式。简单地说:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@android:color/black" />
<item android:state_focused="true" android:color="@android:color/white" />
<item android:state_pressed="true" android:color="@android:color/white" />
<item android:color="#f8f8f8" />
</selector>
但这对我不起作用。我试过的每个组合都不起作用
我知道它一定继承了一些使它透明的属性,但我不知道是哪一个
任何帮助都将不胜感激。尝试使用android:drawable而不是android:color e、 g
尝试使用android:drawable而不是android:color e、 g
由于按钮具有可绘制的背景,因此您可以对该背景进行着色。但必须注意的是,该背景是半透明的,因此色调不会达到100%。这看起来很不错的全息黑暗主题虽然 以下是您的操作方法:
Drawable bgDrawable = button.getBackground();
ColorMatrix cmDesat = new ColorMatrix();
cmDesat.setSaturation( 0 );
ColorMatrix cm = new ColorMatrix();
cm.set( new float[]{
r, 0, 0, 0, 0,
0, g, 0, 0, 0,
0, 0, b, 0, 0,
0, 0, 0, 1, 0
} );
cmDesat.postConcat( cm );
bgDrawable.setColorFilter( new ColorMatrixColorFilter( cmDesat ) );
尝试使用矩阵进行实验,因为它可以产生不同的结果。此外,您可以使用LightingColorFilter代替ColorMatrixColorFilter,它更易于操作,但没有那么灵活。由于按钮具有可绘制的背景,因此您可以对该背景进行着色。但必须注意的是,该背景是半透明的,因此色调不会达到100%。这看起来很不错的全息黑暗主题虽然 以下是您的操作方法:
Drawable bgDrawable = button.getBackground();
ColorMatrix cmDesat = new ColorMatrix();
cmDesat.setSaturation( 0 );
ColorMatrix cm = new ColorMatrix();
cm.set( new float[]{
r, 0, 0, 0, 0,
0, g, 0, 0, 0,
0, 0, b, 0, 0,
0, 0, 0, 1, 0
} );
cmDesat.postConcat( cm );
bgDrawable.setColorFilter( new ColorMatrixColorFilter( cmDesat ) );
尝试使用矩阵进行实验,因为它可以产生不同的结果。另外,您可以简单地使用LightingColorFilter,而不是ColorMatrixColorFilter,它更易于处理,但没有那么灵活