1.

mybutton_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"
android:drawable="@drawable/button_background_focus" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/button_background_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/button_background_pressed" />
<item drawable="@drawable/button_background_normal">
</selector>
android:background="@drawable/mybutton_background"

2.

public class OnPressButton extends Button{

public OnPressButton(Context context) {
super(context);
}

public OnPressButton(Context context, AttributeSet attrs){
super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
//sets the button image based on whether the button in its pressed state
setBackgroundDrawable(getResources().getDrawable(isPressed()?R.drawable.btn_on : R.drawable.btn_off));
super.onDraw(canvas);
}

}

<view
class="com.mycompany.android.ui.OnPressButton"
android:background="@android:color/transparent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Button" />

这句话要注意android:background="@android:color/transparent"

也可以查看http://code.google.com/android/toolbox/custom-components.html

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)APK安装完成自动删除安装包
  2. 《Android平台开发之旅》学习笔记
  3. android sqlite查询数据时报错: get fiel
  4. Android架构模式二:MVP
  5. Android之getSystemService
  6. 浅谈J2me游戏如何快速移植到Android
  7. Android实现左右滑动指引效果
  8. android keyboard
  9. android后台截屏实现(3)--编译screencap
  10. NDK下编译FFMPEG