- 浏览: 1504843 次
- 性别:
- 来自: 南京
文章分类
- 全部博客 (419)
- XMPP (19)
- Android (180)
- Java (59)
- Network (4)
- HTML5 (13)
- Eclipse (9)
- SCM (23)
- C/C++ (4)
- UML (4)
- Libjingle (15)
- Tools&Softwares (29)
- Linphone (5)
- Linux&UNIX (6)
- Windows (18)
- Google (10)
- MISC (3)
- SIP (6)
- SQLite (5)
- Security (4)
- Opensource (29)
- Online (2)
- 文章 (3)
- MemoryLeak (10)
- Decompile (5)
- Ruby (1)
- Image (1)
- Bat (4)
- TTS&ASR (28)
- Multimedia (1)
- iOS (20)
- Asciiflow - ASCII Flow Diagram Tool.htm (1)
- Networking (1)
- DLNA&UPnP (2)
- Chrome (2)
- CI (1)
- SmartHome (0)
- CloudComputing (1)
- NodeJS (3)
- MachineLearning (2)
最新评论
-
bzhao:
点赞123!
Windows的adb shell中使用vi不乱码方法及AdbPutty -
wahahachuang8:
我觉得这种东西自己开发太麻烦了,就别自己捣鼓了,找个第三方,方 ...
HTML5 WebSocket 技术介绍 -
obehavior:
view.setOnTouchListenerview是什么
[转]android 一直在最前面的浮动窗口效果 -
wutenghua:
[转]android 一直在最前面的浮动窗口效果 -
zee3.lin:
Sorry~~
When I build "call ...
Step by Step about How to Build libjingle 0.4
In Android, if you provide custom background images for buttons, you will lose the pressed and disabled image effects. The common way to fix that is to provide additional images for those states. I’m lazy and I find this inconvenient especially during the prototyping phase of app development.
I’ve always liked the way iOS automatically handles pressed and disabled states for custom button backgrounds so I made a Button
subclass that automatically darkens the background image when the
button is pressed, and makes the background transparent when it is
disabled. This is done by using a custom LayerDrawable
for the button that contains the original background Drawable
. The LayerDrawable
has to be stateful
and should change the background properties depending on the current state in onStateChange()
. The full source explains it better:
package net.shikii.widgets; 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.util.AttributeSet; import android.widget.Button; /** * Applies a pressed state color filter or disabled state alpha for the button's background * drawable. * * @author shiki */ public class SAutoBgButton extends Button { public SAutoBgButton(Context context) { super(context); } public SAutoBgButton(Context context, AttributeSet attrs) { super(context, attrs); } public SAutoBgButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setBackgroundDrawable(Drawable d) { // Replace the original background drawable (e.g. image) with a LayerDrawable that // contains the original drawable. SAutoBgButtonBackgroundDrawable layer = new SAutoBgButtonBackgroundDrawable(d); super.setBackgroundDrawable(layer); } /** * The stateful LayerDrawable used by this button. */ protected class SAutoBgButtonBackgroundDrawable extends LayerDrawable { // The color filter to apply when the button is pressed protected ColorFilter _pressedFilter = new LightingColorFilter(Color.LTGRAY, 1); // Alpha value when the button is disabled protected int _disabledAlpha = 100; public SAutoBgButtonBackgroundDrawable(Drawable d) { super(new Drawable[] { d }); } @Override protected 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); } invalidateSelf(); return super.onStateChange(states); } @Override public boolean isStateful() { return true; } } }
To use this, just replace your original button declarations like this:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_blue_bg" android:text="Button with background image" />
To this:
<net.shikii.widgets.SAutoBgButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_blue_bg" android:text="Button with background image" />
Here’s a sample output using this custom button:
The code is also available on GitHub .
Reference:
发表评论
-
[Android] 为Android安装BusyBox —— 完整的bash shell
2013-12-27 10:19 1490http://www.cnblogs.com/xiaowen ... -
Windows的adb shell中使用vi不乱码方法及AdbPutty
2013-12-27 10:17 7577http://www.veryhuo.com/down/ht ... -
AppMobi推出新XDK,可创建测试PhoneGap项目
2012-09-03 13:39 2637AppMobi今天发布了一个新的工具PhoneGap Mobi ... -
Sencha
2012-09-03 12:59 1185http://www.sencha.com/ Se ... -
jQuery Mobile学习
2012-09-01 12:33 1691使用Jquery Mobile设计Android通讯录 ... -
BackBone
2012-09-01 12:34 1260Backbone.js 是一种重量级javascript M ... -
jQTouch
2012-08-30 15:57 984A Zepto/jQuery plugin for mobil ... -
SwiFTP
2012-08-30 15:43 1307SwiFTP is a FTP server that run ... -
kWS
2012-08-30 15:41 1198kWS is a lightweight and fast W ... -
jQuery Mobile
2012-08-30 15:07 1027http://jquerymobile.com/ -
PhoneGap
2012-08-30 15:07 1045http://phonegap.com/ -
[AndriodTips]Image, saved to sdcard, doesn't appear in Android's Gallery app
2012-08-04 16:15 1159http://stackoverflow.com/questi ... -
Voice detection for Android
2012-07-23 11:39 2347Here it is, my fist JAVA applic ... -
[AndroidTip]local reference table overflow (max=512)的错误解决
2012-07-22 22:56 6049JNI层coding经常会遇到ReferenceTable o ... -
[AndroidTip]EditText如何初始状态不获得焦点?
2012-07-22 15:35 1226最简单的办法是在EditText前面放置一个看不到的Linea ... -
[AndroidTip]android textview滚动条
2012-07-21 14:29 1298本来是想做一个显示文字信息的,当文字很多时View的高度不能超 ... -
Google公布Android 4.1完整功能
2012-07-16 09:48 3185http://www.android.com/about/je ... -
Android开发:使用AudioTrack播放PCM音频数据【附源码】
2012-07-13 15:20 20869http://www.linuxidc.com/Linux/2 ... -
Android上的行车记录仪
2012-07-11 22:31 2010MyCar Recorder DailyRoads -
Google hired one of Nuance soft engineers to help work around all Nuance patents
2012-07-10 14:33 1100很有趣的消息: http://forums.macrumor ...
相关推荐
android:background="@color/button_color_selector"/> ``` 3. **渐变色**:可以使用`GradientDrawable`来自定义渐变背景。在代码中或XML中都可以实现。 代码示例: ```java GradientDrawable gradient = new ...
<item android:drawable="@drawable/button_pressed" android:state_pressed="true" /> <!-- 按下状态 --> <item android:drawable="@drawable/button_focused" android:state_focused="true" /> <!-- 获得焦点...
android:background="@drawable/button_color_selector" /> ``` 通过这种方式,Button就会根据其当前状态自动显示相应的颜色。当用户触摸按钮时,它会变为`button_pressed_color`,手指离开后恢复为`button_...
<item android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/button_normal_shape"/> <!-- 按钮按下状态 --> <item android:state_pressed="true" android:drawable="@...
android:background="@drawable/button_states" /> ``` 当按钮的状态改变时,系统会自动根据`button_states.xml`中的规则选择相应的`drawable`,从而改变按钮的外观。这个方法不仅限于颜色变化,还可以用于显示...
<item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <!-- focused --> ...
<item name="android:background">@drawable/button_background_pressed</item> </style> ``` 这里,我们定义了一个名为`CustomButton`的样式,其父样式为`Widget.AppCompat.Button`,这样可以继承系统默认的按钮...
<item android:state_pressed="true" android:drawable="@drawable/switch_button_left_checked"/> <item android:state_focused="true" android:drawable="@drawable/switch_button_left_checked"/> <item ...
在 Android 中, Button 控件的样式可以通过修改其 Background 属性来实现。首先,需要创建一个 XML 文件,类型选 Drawable,根结点选 selector,文件名可以任意命名,例如 button_style。在这个文件中,需要定义三...
android:background="@drawable/button_image" /> ``` 在代码中,可以使用`setBackgroundResource(int resid)`方法: ```java Button myButton = findViewById(R.id.myButton); myButton.setBackgroundResource(R...
<item android:drawable="@drawable/button_pressed" android:state_pressed="true" /> <item android:drawable="@drawable/button_focused" android:state_focused="true" /> <item android:drawable="@drawable...
<item android:state_pressed="true" android:drawable="@drawable/button_pressed"/> <item android:state_focused="true" android:drawable="@drawable/button_focused"/> <item android:drawable="@drawable/...
<item name="android:background">@drawable/button_background</item> <item name="android:padding">8dp</item> <item name="android:focusable">true</item> <item name="android:focusableInTouchMode">true...
<item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <!-- 按下状态 --> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <!-- 聚焦状态 --...
<item android:state_pressed="true" android:drawable="@color/button_pressed_color" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@color/button_focused_color" /> <!-- ...
android:background="@drawable/selector_button" /> ``` Selector不仅限于图片,还可以结合颜色或者渐变效果,使得UI设计更加灵活。例如,我们可以使用ColorStateList来根据按钮状态改变其背景颜色: ```xml ...
<item android:state_pressed="true" android:drawable="@drawable/button_highlight_ctrl" /> <item android:drawable="@drawable/button_normal_ctrl" /> </selector> ``` 当按钮被按下时,它将显示`@drawable/...
例如,可以在`res/layout`目录下的布局文件中定义一个Button,通过`android:text`属性设置按钮文本,`android:background`属性指定背景资源,`android:layout_width`和`android:layout_height`定义按钮尺寸。...
<item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <!-- 按下状态 --> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <!-- 获得焦点...
要应用这些资源到Button控件上,我们需要在布局XML文件中定义Button,并通过`android:background`属性指定背景资源。例如: ```xml <Button android:id="@+id/myButton" android:layout_width="wrap_content" ...