【android开发笔记】为Button的背景图片添加边框式样式效果
2017-02-20 18:19
656 查看
现在做的项目遇到一个问题,设计给过来的图片只有一种状态,但是实现的需求是要求有两个状态,另一种选状态为图片背景加边框。如图:
刚开使用使用ImageView ,ImageViewButton 效果不是很明显;
后来发现 layer-list 能很好的实现这个效果,先分别建 正常模式与选中模式的xml文件:
然后在布局里的内部控件使用:
基本这样可以实现了这个效果!
刚开使用使用ImageView ,ImageViewButton 效果不是很明显;
后来发现 layer-list 能很好的实现这个效果,先分别建 正常模式与选中模式的xml文件:
正常模式:btn_angle_normal_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/transparent_half" /> <stroke android:width="@dimen/dimen_6px" android:color="@color/transparent_half" /> <padding android:bottom="0.0dip" android:left="0.0dip" android:right="0.0dip" android:top="0.0dip" /> </shape>
选中模式:btn_angle_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/transparent_half" /> <stroke android:width="@dimen/dimen_6px" android:color="@color/gold" /> <padding android:bottom="0.0dip" android:left="0.0dip" android:right="0.0dip" android:top="0.0dip" /> </shape>
Selector :common_recangle_bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_angle_bg" android:state_pressed="true" /> <item android:drawable="@drawable/btn_angle_bg" android:state_selected="true" /> <item android:drawable="@drawable/btn_angle_normal_bg" android:state_enabled="true" /> </selector>
Layer-list文件:zhuang_btn.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/common_recangle_bg" /> //这里是normal与press的xml文件 <item android:bottom="@dimen/dimen_4px" android:drawable="@mipmap/bai_table_zhuang_up" //这里是背景图片 android:left="@dimen/dimen_4px" android:right="@dimen/dimen_4px" android:top="@dimen/dimen_4px" /> </layer-list>
然后在布局里的内部控件使用:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/zhuang_btn" android:gravity="bottom|center" android:text="4545\n" android:textSize="@dimen/dimen_tv_20" />
基本这样可以实现了这个效果!
相关文章推荐
- Android开发笔记(九十八)往图片添加部件
- WPF学习笔记——为BUTTON添加背景图片
- WPF学习笔记——为BUTTON添加背景图片
- Android应用开发笔记(10):制作自定义背景Button按钮、自定义形状Button的全攻略
- Android应用开发笔记(10):制作自定义背景Button按钮、自定义形状Button的全攻略
- Android画板开发(四) 添加背景和保存画板内容为图片
- Android 笔记:ImageButton单击切换按钮图片效果的实现(转)
- android开发步步为营之58:给图片绘制圆形气泡背景效果
- android 界面 按button或者imagebutton改变图片或者背景的效果
- Android应用开发笔记(10):制作自定义背景Button按钮、自定义形状Button的全攻略
- 【Android游戏开发之七】(游戏开发中需要的样式)再次剖析游戏开发中对SurfaceView中添加组件方案!
- Android笔记- 使用XML selector 设置按钮点击效果图片
- Android背景添加渐变,描边,圆角的效果
- Android开发笔记(一百五十)自动识别验证码图片
- Android开发_如何设置按钮背景透明与半透明_图片背景透明
- Android 给图片、文字、控件、布局添加阴影效果(悬浮效果)
- Android开发笔记——图片缓存、手势及OOM分析
- android 界面开发: 图片、界面滑动翻页效果(ViewPager)
- Android根据Button状态(normal,focused,pressed)显示不同背景图片