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; } }
截图如下:
點(diǎn)擊查看更多內(nèi)容
為 TA 點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦