在android开发中,我们经常要自定义组件如TextView等背景或边框为四角圆形。

首先,我们在drawable目录下新建一个xml文件,bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"><!-- android:shape="" 表示是圆角矩形还是椭圆等等 -->
<!-- 背景颜色 -->
<solid android:color="@color/mycolor" />

<!-- padding 表示内部空间距离背景图片内部边距 的距离 -->
<padding android:top="15dp" android:bottom="15dp" />

<!-- 图片边框属性 -->
<stroke
android:width="0dp"
android:color="#ffffff" />


<!-- 圆角的程度 -->
<corners android:radius="5dp" />

<!--定义背景的渐变 如果是不渐变可不写-->

<gradient
android:angle="45"<!---angle表示方向角度。当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。->
android:centerColor="@color/center"<!--渐变色的中间颜色-->
android:centerX="0.5" <!--渐变的中间 0.5为x轴方向的中间-->
android:centerY="0.5"
android:endColor="@color/endcolor
android:startColor="@color/startcolor"
android:type="linear" />

<!--linear 线性渐变,这是默认设置

radial 放射性渐变,以开始色为中心。

sweep 扫描线式的渐变。-->


</shape>

这样,一个四角圆形的背景就完成了,只需要在view中设置android:background="@drawable/bg"

就可以了。

关于设置按下时背景的改变,请参考另一篇文章http://dzc.iteye.com/blog/2161737

更多相关文章

  1. android 设置textview 边框
  2. Android EditText无边框问题
  3. android 背景圆角以及图片圆角处理
  4. android GridView 去掉自带点击边框效果和禁止上下滑动
  5. Android 解决自定义 CheckBox 样式时的背景显示异常问题
  6. 浅谈android的selector,背景选择器 .

随机推荐

  1. android开源库合集
  2. LinearLayout中android:layout_gravity="
  3. android:layout_weight属性详解
  4. AMD windows7安装和打开genymotion、andr
  5. Android的动画
  6. Android(安卓)requires compiler complia
  7. Android(安卓)Studio导入项目的gradle与s
  8. Android:安装APK包以后系统文件目录的变
  9. Android判断定位功能是否开启
  10. IDEA 快捷键 Android(安卓)Studio快捷键