现在做的项目遇到一个问题,设计给过来的图片只有一种状态,但是实现的需求是要求有两个状态,另一种选状态为图片背景加边框。如图:

刚开使用使用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开发笔记】为Button的背景图片添加边框式样式效果的更多相关文章

  1. android图像处理系列之四-- 给图片添加边框(上)

    图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...

  2. android图像处理系列之五-- 给图片添加边框(中)

    前面一篇讲到给图片加边框的方式,只能给图片加一些有规则的边框,如果想加一些比较精美的效果,就有点麻烦了.下面就给出解决这个问题的思路. 思路是:一些比较精美的花边图片我们是很难用代码控制,就目前本人水 ...

  3. android图像处理系列之四--给图片添加边框(上)

    图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...

  4. android图像处理系列之五--给图片添加边框(中)

    前面一篇讲到给图片加边框的方式,只能给图片加一些有规则的边框,如果想加一些比较精美的效果,就有点麻烦了.下面就给出解决这个问题的思路. 思路是:一些比较精美的花边图片我们是很难用代码控制,就目前本人水 ...

  5. Android 开发笔记___SD卡基本操作__图片读取写入

    package com.example.alimjan.hello_world.Utils; import android.graphics.Bitmap; import android.graphi ...

  6. Android学习笔记进阶19 之给图片加边框

    //设置颜色 public void setColour(int color){ co = color; } //设置边框宽度 public void setBorderWidth(int width ...

  7. Android学习笔记进阶19之给图片加边框

    //设置颜色 public void setColour(int color){ co = color; } //设置边框宽度 public void setBorderWidth(int width ...

  8. android图像处理系列之六-- 给图片添加边框(下)-图片叠加

    前面介绍了一种用透明图片叠加的方式添加花边边框,下面将介绍另外一种图片叠加添加花边边框的方式.前面方法有一个缺点,就是做成PNG图片,图片体积会很大,不是一般的大,比同样的JPG大三倍多,如果项目可以 ...

  9. android图像处理系列之六--给图片添加边框(下)-图片叠加

    前面介绍了一种用透明图片叠加的方式添加花边边框,下面将介绍另外一种图片叠加添加花边边框的方式.前面方法有一个缺点,就是做成PNG图片,图片体积会很大,不是一般的大,比同样的JPG大三倍多,如果项目可以 ...

随机推荐

  1. bzoj4008: [HNOI2015]亚瑟王【期望dp】

    一个特别神奇的dp,特别厉害. f(i, j) 表示 有 j 轮发动技能的牌在 [1, i] 另外的m - j轮在[i + 1, n]之间的概率. 怎么转移呢? 首先考虑i这张牌不选的情况,f(i - ...

  2. delphi显示hello world 和退出程序

    Label1.Caption:='hello world!' Form1.close; application.Terminate; //终止程序 Application.Run; //程序运行 te ...

  3. javascript中的__proto__和prototype

    一.2个参考网址: http://icekiller110.iteye.com/blog/1566768 http://www.cnblogs.com/snandy/archive/2012/09/0 ...

  4. python获取绑定的IP,并动态指定出口IP

    在做采集器的过程中,经常会遇到IP限制的情况,这时候可以通过切换IP能继续访问. 如果是多IP的服务器,那么可以通过切换出口Ip来实现. 1.首先是如何获取服务器绑定的IP import netifa ...

  5. 黄聪:VPS用轻松备份工具备份Wordpress,文件夹通配符

    db;log;wp-admin;wp-includes;temp;upgrade;twentyfourteen;twentyfifteen;twentysixteen;twentythirteen;t ...

  6. 绘图——Android绘图基础:Canvas、Paint等

    Android的绘图应该继承View组件,并重写它的onDraw(Canvas canvas)方法即可. 重写onDraw(Canvas canvas)方法时涉及一个绘图API:Canvas,Canv ...

  7. swift button一些简单设置

    1,按钮的创建(1)按钮有下面四种类型: UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果 UIButtonType.DetailDiscl ...

  8. redis 高级配置

    一.安全性 设置密码:在配置文件中设置 requirepass 123456 由于redis的速度非常快,每秒可以进行15万次的暴力破解,所以密码设置要强壮些 在客户端登录或者连接的时候,使用 aut ...

  9. centos 单独安装nginx

    安装包下载网址: http://nginx.org/en/download.html 进入Linux文件夹执行 wget http://nginx.org/download/nginx-1.8.0.t ...

  10. JavaSE学习总结(一)——Java基础

    一.Java是什么 Java 是由 Sun Microsystems 在 1995 年首先发布的编程语言和计算平台.Java 是一项用于开发应用程序的技术,可以让 Web 变得更有意思和更实用.有许多 ...