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;
}
}
截图如下:
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
