1、用xml文件实现圆角
a、在drawable文件夹下新建shape.xml文件,内如如下:
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--Button正常状态下的背景色--> <solid android:color="#A3B1B2" /> <!--设置圆角的半径--> <corners android:radius="10dip"/> </shape>
b、再新建一个shape_press.xml
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--Button触摸状态下的背景色--> <solid android:color="#8C9899" /> <!--设置圆角的半径--> <corners android:radius="10dip"/> </shape>
2、在java代码中设置Buttond的触摸事件,使用不同的xml资源实现点击效果
public class ButtonOnTouchListener implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(v.getId()==R.id.button1){
Button b = (Button)v;
if(event.getAction()==MotionEvent.ACTION_DOWN){
//触摸时,调用shape_press.xml资源
b.setBackgroundResource(R.drawable.shape_press);
}else if(event.getAction()==MotionEvent.ACTION_UP){
//释放时,调用shape.xml资源
b.setBackgroundResource(R.drawable.shape);
}
}
return false;
}
}
截图如下:

随时随地看视频