标准Android按钮具有不同的颜色
我想稍微更改标准Android按钮的颜色,以便更好地匹配客户的品牌。
到目前为止,我发现这样做的最好方法是将Button
的drawable更改为res/drawable/red_button.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@drawable/red_button_pressed" /><item android:state_focused="true" android:drawable="@drawable/red_button_focus" /><item android:drawable="@drawable/red_button_rest" />
</selector>
但这样做需要我实际上为我想要自定义的每个按钮创建三个不同的drawable(一个用于静止按钮,一个用于聚焦,一个用于按下)。 这似乎比我需要的更复杂,更干燥。
我真正想做的就是对按钮应用某种颜色转换。 是否有更简单的方法来改变按钮的颜色而不是我正在做的事情?
#1楼
我喜欢@conjugatedirection和@Tomasz先前答案中的滤色器建议; 但是,我发现到目前为止提供的代码并不像我预期的那样容易应用。
首先,它没有提到在何处应用和清除彩色滤光片。 有可能还有其他好的地方可以做到这一点,但我想到的是一个OnTouchListener 。
从我对原始问题的解读,理想的解决方案将是不涉及任何图像的解决方案。 如果这是你的目标,那么使用@emmby中的custom_button.xml接受的答案可能比彩色滤镜更合适。 在我的例子中,我从一个UI设计师的png图像开始,按钮应该是什么样子。 如果我将按钮背景设置为此图像,则默认高光反馈将完全丢失。 此代码使用程序化变暗效果替换该行为。
button.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:// 0x6D6D6D sets how much to darken - tweak as desiredsetColorFilter(v, 0x6D6D6D);break;// remove the filter when moving off the button// the same way a selector implementation would case MotionEvent.ACTION_MOVE:Rect r = new Rect();v.getLocalVisibleRect(r);if (!r.contains((int) event.getX(), (int) event.getY())) {setColorFilter(v, null);}break;case MotionEvent.ACTION_OUTSIDE:case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_UP:setColorFilter(v, null);break;}return false;}private void setColorFilter(View v, Integer filter) {if (filter == null) v.getBackground().clearColorFilter();else {// To lighten instead of darken, try this:// LightingColorFilter lighten = new LightingColorFilter(0xFFFFFF, filter);LightingColorFilter darken = new LightingColorFilter(filter, 0x000000);v.getBackground().setColorFilter(darken);}// required on Android 2.3.7 for filter change to take effect (but not on 4.0.4)v.getBackground().invalidateSelf();}
});
我把它作为一个单独的类提取出来应用于多个按钮 - 显示为匿名内部类只是为了得到这个想法。
#2楼
现在有一种更简单的方法: android-holo-colors.com
它可以让你轻松改变所有holo drawables(纽扣,旋转器......)的颜色。 您选择颜色,然后下载包含所有分辨率的drawable的zip文件。
#3楼
我发现这一切都可以很容易地在一个文件中完成。 将类似下面的代码放在名为custom_button.xml
的文件中,然后在按钮视图中设置background="@drawable/custom_button"
:
<?xml version="1.0" encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" ><shape><gradientandroid:startColor="@color/yellow1"android:endColor="@color/yellow2"android:angle="270" /><strokeandroid:width="3dp"android:color="@color/grey05" /><cornersandroid:radius="3dp" /><paddingandroid:left="10dp"android:top="10dp"android:right="10dp"android:bottom="10dp" /></shape></item><item android:state_focused="true" ><shape><gradientandroid:endColor="@color/orange4"android:startColor="@color/orange5"android:angle="270" /><strokeandroid:width="3dp"android:color="@color/grey05" /><cornersandroid:radius="3dp" /><paddingandroid:left="10dp"android:top="10dp"android:right="10dp"android:bottom="10dp" /></shape></item><item> <shape><gradientandroid:endColor="@color/blue2"android:startColor="@color/blue25"android:angle="270" /><strokeandroid:width="3dp"android:color="@color/grey05" /><cornersandroid:radius="3dp" /><paddingandroid:left="10dp"android:top="10dp"android:right="10dp"android:bottom="10dp" /></shape></item>
</selector>
#4楼
在<Button>
使用android:background="#33b5e5"
。 或更好的android:background="@color/navig_button"
#5楼
您还可以使用此在线工具自定义按钮http://angrytools.com/android/button/并使用android:background="@drawable/custom_btn"
来定义布局中的自定义按钮。
#6楼
一种简单的方法是只定义一个自定义Button类,它接受你想要的所有属性,如半径,渐变,按下的颜色,正常颜色等,然后在XML布局中使用它而不是使用XML设置背景。 样本是这里
如果您有许多具有相同属性(如半径,选定颜色等)的按钮,则此功能非常有用。您可以自定义继承按钮以处理这些附加属性。
结果(未使用背景选择器)。
普通按钮
按下按钮
#7楼
以这种方式使用它:
buttonOBJ.getBackground().setColorFilter(Color.parseColor("#YOUR_HEX_COLOR_CODE"), PorterDuff.Mode.MULTIPLY);
#8楼
我正在使用这种方法
style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"><item name="android:colorPrimaryDark">#413152</item><item name="android:colorPrimary">#534364</item><item name="android:colorAccent">#534364</item><item name="android:buttonStyle">@style/MyButtonStyle</item>
</style><style name="MyButtonStyle" parent="Widget.AppCompat.Button.Colored"><item name="android:colorButtonNormal">#534364</item><item name="android:textColor">#ffffff</item>
</style>
从上面可以看出,我正在为我的按钮使用自定义样式。 按钮颜色对应于强调颜色。 我发现这比设置android:background
更好的方法,因为我不会失去Google提供的涟漪效应。
#9楼
迈克,您可能对彩色滤光片感兴趣。
一个例子:
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
试试这个来达到你想要的颜色。
#10楼
您现在还可以将appcompat-v7的AppCompatButton与backgroundTint
属性一起使用:
<android.support.v7.widget.AppCompatButtonxmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="wrap_content"app:backgroundTint="#ffaa00"/>
#11楼
继Tomasz的回答之后,您还可以使用PorterDuff乘法模式以编程方式设置整个按钮的阴影。 这将改变按钮颜色而不仅仅是色调。
如果您使用标准灰色阴影按钮开头:
button.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
会给你一个红色阴影按钮,
button.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
会给你一个绿色阴影按钮等,其中第一个值是十六进制格式的颜色。
它的工作原理是将当前按钮颜色值乘以颜色值。 我相信你可以用这些模式做更多的事情。
#12楼
这是我从API 15开始完美运行的解决方案。 此解决方案保留所有默认按钮单击效果,如材质RippleEffect
。 我没有在较低的API上测试它,但它应该工作。
你需要做的就是:
1)创建一个仅更改colorAccent
的样式:
<style name="Facebook.Button" parent="ThemeOverlay.AppCompat"><item name="colorAccent">@color/com_facebook_blue</item>
</style>
我建议使用
ThemeOverlay.AppCompat
或您的主AppTheme
作为父级,以保留其余的样式。
2)将这两行添加到button
小部件:
style="@style/Widget.AppCompat.Button.Colored"
android:theme="@style/Facebook.Button"
有时您的新
colorAccent
未在Android Studio预览中显示,但是当您在手机上启动应用时,颜色将会更改。
示例按钮小部件
<Buttonandroid:id="@+id/sign_in_with_facebook"style="@style/Widget.AppCompat.Button.Colored"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:text="@string/sign_in_facebook"android:textColor="@android:color/white"android:theme="@style/Facebook.Button" />
#13楼
我做一个非常好的按钮样式按钮的方式是子类Button对象并应用颜色过滤器。 这也通过对按钮应用alpha来处理启用和禁用状态。
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.Button;public class DimmableButton extends Button {public DimmableButton(Context context) {super(context);}public DimmableButton(Context context, AttributeSet attrs) {super(context, attrs);}public DimmableButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@SuppressWarnings("deprecation")@Overridepublic void setBackgroundDrawable(Drawable d) {// Replace the original background drawable (e.g. image) with a LayerDrawable that// contains the original drawable.DimmableButtonBackgroundDrawable layer = new DimmableButtonBackgroundDrawable(d);super.setBackgroundDrawable(layer);}@TargetApi(Build.VERSION_CODES.JELLY_BEAN)@Overridepublic void setBackground(Drawable d) {// Replace the original background drawable (e.g. image) with a LayerDrawable that// contains the original drawable.DimmableButtonBackgroundDrawable layer = new DimmableButtonBackgroundDrawable(d);super.setBackground(layer);}/*** The stateful LayerDrawable used by this button.*/protected class DimmableButtonBackgroundDrawable extends LayerDrawable {// The color filter to apply when the button is pressedprotected ColorFilter _pressedFilter = new LightingColorFilter(Color.LTGRAY, 1);// Alpha value when the button is disabledprotected int _disabledAlpha = 100;// Alpha value when the button is enabledprotected int _fullAlpha = 255;public DimmableButtonBackgroundDrawable(Drawable d) {super(new Drawable[] { d });}@Overrideprotected boolean onStateChange(int[] states) {boolean enabled = false;boolean pressed = false;for (int state : states) {if (state == android.R.attr.state_enabled)enabled = true;else if (state == android.R.attr.state_pressed)pressed = true;}mutate();if (enabled && pressed) {setColorFilter(_pressedFilter);} else if (!enabled) {setColorFilter(null);setAlpha(_disabledAlpha);} else {setColorFilter(null);setAlpha(_fullAlpha);}invalidateSelf();return super.onStateChange(states);}@Overridepublic boolean isStateful() {return true;}}}
#14楼
您可以将按钮的主题设置为此
<style name="AppTheme.ButtonBlue" parent="Widget.AppCompat.Button.Colored"><item name="colorButtonNormal">@color/HEXColor</item><item name="android:textColor">@color/HEXColor</item>
</style>
#15楼
值\\ styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item>
</style><style name="RedAccentButton" parent="ThemeOverlay.AppCompat.Light"><item name="colorAccent">#ff0000</item>
</style>
然后:
<Buttonstyle="@style/Widget.AppCompat.Button.Colored"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="text" /><Buttonstyle="@style/Widget.AppCompat.Button.Colored"android:layout_width="match_parent"android:layout_height="wrap_content"android:enabled="false"android:text="text" /><Buttonstyle="@style/Widget.AppCompat.Button.Colored"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="text"android:theme="@style/RedAccentButton" /><Buttonstyle="@style/Widget.AppCompat.Button.Colored"android:layout_width="match_parent"android:layout_height="wrap_content"android:enabled="false"android:text="text"android:theme="@style/RedAccentButton" />
#16楼
适用于任何Android版本的最短解决方案:
<Buttonapp:backgroundTint="@color/my_color"
备注/要求 :
- 使用
app:
namespace而不是android:
namespace! appcompat版本> 24.2.0
依赖{compile'com.android.support:appcompat-v7:25.3.1'}
说明 :
#17楼
根据材料设计指南,您需要使用如下代码的样式
<style name="MyButton" parent="Theme.AppCompat.Light><item name="colorControlHighlight">#F36F21</item><item name="colorControlHighlight">#FF8D00</item>
</style>
并在布局中将此属性添加到您的按钮
android:theme="@style/MyButton"
#18楼
它很简单..在你的项目中添加这个依赖项并创建一个带有1的按钮。任何形状2.任何颜色3.任何边框4.具有材质效果
https://github.com/manojbhadane/QButton
<com.manojbhadane.QButtonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="OK"app:qb_backgroundColor="@color/green"app:qb_radius="100"app:qb_strokeColor="@color/darkGreen"app:qb_strokeWidth="5" />
#19楼
DroidUX组件库有一个ColorButton
小部件,其颜色可以通过xml定义和运行时以编程方式轻松更改,因此您甚至可以让用户在应用程序允许的情况下设置按钮的颜色/主题。
#20楼
如果您使用XML制作颜色按钮,则可以通过在单独的文件中指定聚焦和按下状态并重复使用来使代码更清晰。 我的绿色按钮看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_focused="true" android:drawable="@drawable/button_focused"/><item android:state_pressed="true" android:drawable="@drawable/button_pressed"/><item><shape><gradient android:startColor="#ff00ff00" android:endColor="#bb00ff00" android:angle="270" /><stroke android:width="1dp" android:color="#bb00ff00" /><corners android:radius="3dp" /><padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /></shape></item></selector>
标准Android按钮具有不同的颜色相关推荐
- android按钮背景颜色函数,Android按钮背景颜色(Android button background color)
Android按钮背景颜色(Android button background color) 我想在我的应用程序中设置按钮的背景颜色,我无法实现我想要的结果... 我试图设置的颜色是holo_gree ...
- 改变Android按钮背景颜色的高效方法
本文将介绍一种有效改变Android按钮颜色的方法. 按钮可以在状态改变时改变其颜色(例如按下,禁用,高亮显示).但是,这需要一一说明每个状态.这篇文章将提供你一个根据状态变化轻松改变按钮颜色的方法. ...
- Android按钮设计,自定义按钮,圆形按钮,颜色
In this tutorial, we'll be customizing the Buttons in our Android Application. If you aren't aware o ...
- android按钮阴影效果图,android – 如何设置按钮有图像和阴影?
要制作阴影,制作阴影xml文件并使用像android:background ="@ drawable / shadow". 但是把图像放到android:background =& ...
- 【Android 应用开发】Android - 按钮组件详解
总结了Android中常用的按钮用法 示例源码下载地址 : -- CSDN : http://download.csdn.net/detail/han1202012/6852091 -- GitHu ...
- android按钮控件常见问题,Android的基本控件和Activity的应用总结
Android的基本控件 常用界面控件 TextView 显示文本信息 button 普通按钮 EditText 可编辑的文本框组件(输入框) ImageView 用于显示图片 ImageBUtton ...
- android控件字体,android 设置控件的颜色字体的方法
1.用代码设置控件的颜色: int b = getResources().getColor(R.drawable.blue);//得到配置文件里的颜色 mButton.setTextColor(b) ...
- android按钮_Android按钮
android按钮 In this tutorial, we will look into Android Button. We will create android studio applicat ...
- android按钮美化(入门)
android按钮美化 话不多说,上运行效果图 在drawable文件夹下 新建button_drawable.xml <?xml version="1.0" encodin ...
最新文章
- 使用Ef框架进行分页..EF模糊查询..EF多表内连接查询
- TensorFlow学习笔记(一):数据操作指南
- CentOS虚拟机克隆后网卡配置问题
- Apache httpd Server 配置正向代理
- 成功解决AttributeError: ‘Series‘ object has no attribute ‘columns‘
- 【模板】Manacher算法
- LeetCode 1711. 大餐计数(map计数 + 二分查找)
- SQL 强制指定索引加快查询速度
- 【一类题】二维数点的几个做法
- 该服务器支持最多2100个参数,Mybatis批量查询拼装参数超长的解决办法
- 手机modem开发(1)---MTK modem开发
- goroutine 修改全局变量无效问题
- 计算几何——判断点是否在多边形内
- kaggle Notebook Threw Exception问题解决
- 人工智能系列电子书分享
- Elsevier 投稿各种状态总结
- delphi11中使用python4delphi组件
- Aspose.3D使用教程:使用 Java 将 FBX 转换为 RVM 或 RVM 转换为 FBX 文件
- 20130327-[转]讲完这个故事我就要嫁给别人
- 扑克牌java发牌_Java练习——扑克牌发牌器