android 利用shape做控件背景(小圆点,空心带边框背景)

android给控件添加背景可以不用图片资源,简单的纯色背景可以自己画,类似下图:
android 利用shape做控件背景(小圆点,空心带边框背景)_第1张图片
要实现一个只有边框的button,可以直接做图,也可以直接用shape画,代码如下:
`





`
solid是背景,corners可以设置4个角的角度大小,stoke可以设置边框线的宽度和颜色
也可以代码动态设置边框宽度和颜色,代码如下:

 //获取控件的背景图
            Drawable background = btn_next.getBackground();
            //获取背景颜色
            ColorDrawable colorDrawable = (ColorDrawable) background;
            int color = colorDrawable.getColor();
            GradientDrawable mGroupDrawable= (GradientDrawable) btn_find_lockpsw.getBackground();
            /*设置边框颜色和宽度*/
            mGroupDrawable.setStroke(1, color);

如果想实现一个小圆点,可以这么做:



    
    

    

android:shape=“oval”//设置形状为圆
solid //背景色
corners//圆半径
size可以在xml中设置

你可能感兴趣的:(android)