emm*_*mby 728 android android-layout
我想稍微更改标准Android按钮的颜色,以便更好地匹配客户的品牌.
到目前为止,我发现这样做的最好方法是将Button
'drawable'改为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>
Run Code Online (Sandbox Code Playgroud)
但这样做需要我居然为每一个按钮,我想自定义三种不同的可绘制(一个用于按钮在休息,一个集中的时候,和一个按下时).这似乎比我需要的更复杂,更干燥.
我真正想做的就是对按钮应用某种颜色转换.是否有更简单的方法来改变按钮的颜色而不是我正在做的事情?
emm*_*mby 720
我发现这一切都可以很容易地在一个文件中完成.将以下代码放在一个名为的文件中custom_button.xml
,然后background="@drawable/custom_button"
在按钮视图中设置:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="@color/yellow1"
android:endColor="@color/yellow2"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/grey05" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="@color/orange4"
android:startColor="@color/orange5"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/grey05" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="@color/blue2"
android:startColor="@color/blue25"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/grey05" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
小智 306
继Tomasz的回答之后,您还可以使用PorterDuff乘法模式以编程方式设置整个按钮的阴影.这将改变按钮颜色而不仅仅是色调.
如果您使用标准灰色阴影按钮开头:
button.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)
会给你一个红色阴影按钮,
button.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)
会给你一个绿色阴影按钮等,其中第一个值是十六进制格式的颜色.
它的工作原理是将当前按钮颜色值乘以颜色值.我相信你可以用这些模式做更多的事情.
Tom*_*asz 148
迈克,您可能对彩色滤光片感兴趣.
一个例子:
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
Run Code Online (Sandbox Code Playgroud)
试试这个来达到你想要的颜色.
Red*_*ne1 83
这是我从API 15开始完美运行的解决方案.此解决方案保留所有默认按钮单击效果,如材质RippleEffect
.我没有在较低的API上测试它,但它应该工作.
你需要做的就是:
1)创建仅更改的样式colorAccent
:
<style name="Facebook.Button" parent="ThemeOverlay.AppCompat">
<item name="colorAccent">@color/com_facebook_blue</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我建议使用
ThemeOverlay.AppCompat
或您的主要AppTheme
作为父母,以保持其余的风格.
2)将这两行添加到您的button
小部件:
style="@style/Widget.AppCompat.Button.Colored"
android:theme="@style/Facebook.Button"
Run Code Online (Sandbox Code Playgroud)
有时您的新
colorAccent
内容未在Android Studio预览中显示,但是当您在手机上启动应用时,颜色将会更改.
<Button
android: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" />
Run Code Online (Sandbox Code Playgroud)
Nil*_*cem 59
您现在还可以将appcompat-v7的AppCompatButton与backgroundTint
属性一起使用:
<android.support.v7.widget.AppCompatButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:backgroundTint="#ffaa00"/>
Run Code Online (Sandbox Code Playgroud)
Sta*_*iel 22
我喜欢@conjugatedirection和@Tomasz先前答案中的滤色器建议; 但是,我发现到目前为止提供的代码并不像我预期的那样容易应用.
首先,它没有提到的地方申请,并清除彩色滤光片.有可能还有其他好的地方可以做到这一点,但我想到的是一个OnTouchListener.
从我对原始问题的解读,理想的解决方案将是不涉及任何图像的解决方案.如果这是你的目标,那么使用@emmby中的custom_button.xml接受的答案可能比彩色滤镜更合适.在我的例子中,我从一个UI设计师的png图像开始,按钮应该是什么样子.如果我将按钮背景设置为此图像,则默认高光反馈将完全丢失.此代码使用程序化变暗效果替换该行为.
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 0x6D6D6D sets how much to darken - tweak as desired
setColorFilter(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();
}
});
Run Code Online (Sandbox Code Playgroud)
我把它作为一个单独的类提取出来应用于多个按钮 - 显示为匿名内部类只是为了得到这个想法.
小智 16
如果您使用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>
Run Code Online (Sandbox Code Playgroud)
use*_*087 12
适用于任何Android版本的最短解决方案:
<Button
app:backgroundTint="@color/my_color"
Run Code Online (Sandbox Code Playgroud)
备注/要求:
app:
命名空间和没有的android:
命名空间!appcompat版本> 24.2.0
依赖{compile'com.android.support:appcompat-v7:25.3.1'}
我正在使用这种方法
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>
Run Code Online (Sandbox Code Playgroud)
从上面可以看出,我正在为我的按钮使用自定义样式.按钮颜色对应于强调颜色.我发现这是一个比设置更好的方法,android:background
因为我不会失去谷歌提供的涟漪效应.
现在有一种更简单的方法:android-holo-colors.com
它可以让你轻松改变所有holo drawables(纽扣,旋转器......)的颜色.您选择颜色,然后下载包含所有分辨率的drawable的zip文件.
以这种方式使用它:
buttonOBJ.getBackground().setColorFilter(Color.parseColor("#YOUR_HEX_COLOR_CODE"), PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
465193 次 |
最近记录: |