1、在res/drawable目录下创建一个shape.xml

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns:android="http://schemas.android.com/apk/res/android">
  3. <solidandroid:color="#ffffffff"/>
  4. <cornersandroid:radius="10dp"/>
  5. <padding
  6. android:bottom="10dp"
  7. android:left="5dp"
  8. android:right="5dp"
  9. android:top="5dp"/>
  10. <stroke
  11. android:width="2dp"
  12. android:color="#ff00bbe8">
  13. </stroke>
  14. </shape>

2、在需要的地方调用:

[html] view plain copy
  1. <EditText
  2. android:id="@+id/mText"
  3. android:layout_width="fill_parent"
  4. android:layout_height="100px"
  5. android:background="@drawable/shape"
  6. android:textColor="#aa000000"
  7. android:textSize="20dp"
  8. android:gravity="top"
  9. android:layout_margin="3dp"
  10. android:paddingLeft="10dp"
  11. android:paddingRight="10dp"/>

3、看下效果图:

4、关于shape的其他属性:

shape与selector综合使用

[html] view plain copy
  1. button_selector.xml:
  2. <?xmlversion="1.0"encoding="utf-8"?>
  3. <selector
  4. xmlns:android="http://schemas.android.com/apk/res/android">
  5. <itemandroid:state_pressed="true">
  6. <shape>
  7. <!--渐变-->
  8. <gradient
  9. android:startColor="#ff8c00"
  10. android:endColor="#FFFFFF"
  11. android:type="radial"
  12. android:gradientRadius="50"/>
  13. <!--描边-->
  14. <stroke
  15. android:width="2dp"
  16. android:color="#dcdcdc"
  17. android:dashWidth="5dp"
  18. android:dashGap="3dp"/>
  19. <!--圆角-->
  20. <corners
  21. android:radius="2dp"/>
  22. <padding
  23. android:left="10dp"
  24. android:top="10dp"
  25. android:right="10dp"
  26. android:bottom="10dp"/>
  27. </shape>
  28. </item>
  29. <itemandroid:state_focused="true">
  30. <shape>
  31. <gradient
  32. android:startColor="#ffc2b7"
  33. android:endColor="#ffc2b7"
  34. android:angle="270"/>
  35. <stroke
  36. android:width="2dp"
  37. android:color="#dcdcdc"/>
  38. <corners
  39. android:radius="2dp"/>
  40. <padding
  41. android:left="10dp"
  42. android:top="10dp"
  43. android:right="10dp"
  44. android:bottom="10dp"/>
  45. </shape>
  46. </item>
  47. <item>
  48. <shape>
  49. <solidandroid:color="#ff9d77"/>
  50. <stroke
  51. android:width="2dp"
  52. android:color="#fad3cf"/>
  53. <corners
  54. android:topRightRadius="5dp"
  55. android:bottomLeftRadius="5dp"
  56. android:topLeftRadius="0dp"
  57. android:bottomRightRadius="0dp"
  58. />
  59. <padding
  60. android:left="10dp"
  61. android:top="10dp"
  62. android:right="10dp"
  63. android:bottom="10dp"/>
  64. </shape>
  65. </item>
  66. </selector>

solid:实心,就是填充的意思
android:color 指定填充的颜色

gradient:渐变
android:startColor和android:endColor分别为起始和结束颜色,ndroid:angle是渐变角度, 必须为45的整数倍
另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"。

stroke:描边
android:width="2dp" 描边的宽度,android:color 描边的颜色。
我们还可以把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp"
android:dashGap="3dp"
其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。

corners:圆角
android:radius为角的弧度,值越大角越圆。
我们还可以把四个角设定成不同的角度,方法为:
<corners
android:topRightRadius="20dp" 右上角
android:bottomLeftRadius="20dp" 右下角
android:topLeftRadius="1dp" 左上角
android:bottomRightRadius="0dp" 左下角
/>

padding:间隔

更多相关文章

  1. android Canvas drawText 文字居中
  2. Android两行代码修改给系统状态栏设置透明效果
  3. Android培训班(29)
  4. android -- TextView 部分字符带颜色 与 popupwindow 的使用
  5. Android(安卓)8位颜色值和6位颜色值的区别
  6. Android学习札记6:ProgressBar圆形进度条的颜色设置
  7. android实现选择图片并压缩上传的功能
  8. 对hint 设置大小及颜色,可用此方法对字符串设置不同颜色或大小
  9. Android(安卓)TextView 中设置text的格式

随机推荐

  1. Android面试题收集
  2. Android美化EditText
  3. #周末课堂# 赵扬老师 Android系列课程【L
  4. android:shape的使用
  5. Android的Camera架构介绍 (转)
  6. 認識Android的BinderProxy和Binder類別
  7. android关闭应用
  8. Broadcast
  9. AndroidManifest.xml文件剖析 (二)
  10. android 五大布局经典演绎