Android开发系列(十八):自定义控件样式在drawable文件夹下的XML实现
来源:互联网 发布:情义知多少 编辑:程序博客网 时间:2024/04/20 17:06
在Android开发的过程中,我们经常需要对控件的样式做一下改变,可以通过用增加背景图片的方式进行改变,但是背景图片放多了肯定会使得APK文件变的很大。
我们可以用自定义属性shape来实现。
shape:
gradient -- 对应颜色渐变。 startcolor、endcolor就不多说了。 android:angle 是指从哪个角度开始变。
solid -- 填充。
stroke -- 描边。
corners -- 圆角。
padding -- 定义内容离边界的距离。
我们先来看一下效果:
下边我们用具体的代码来演示一下:
首先,新建一个项目,然后我们在main.xml编辑:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_marginTop="10dp" android:text="演示文本" android:textSize="14pt" android:textColor="#565656" android:background="@drawable/bg_border" /> <!-- 通过android:drawableLeft绘制一张图片 --><TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="演示文本2"android:textSize="24pt"android:background="@drawable/bg_border2"/></LinearLayout></span>可以看到,在上边的两个TextView的android:background属性,用到了@drawable/bg_border和@drawable/bg_border2这两个文件
然后,我们在@drawable文件夹下边创建这两个Android xml文件:
bg_border.xml:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 填充:设置背景颜色 --><solid android:color="#ffffff"/><!-- 设置边框 --><stroke android:width="5px" android:color="#873600" android:dashWidth="5dip" /><!-- 定义内边距 --><padding android:top="20dip" /> </shape></span>
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 指定圆角矩形的4个圆角的半径 --> <corners android:topLeftRadius="20px" android:topRightRadius="5px" android:bottomRightRadius="20px" android:bottomLeftRadius="5px"/> <!-- 指定边框线条的宽度和颜色 --><stroke android:width="4px" android:color="#f0f" /><!-- 指定使用渐变背景色,使用sweep类型的渐变颜色从红色→绿色→蓝色 --><gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:type="sweep"/></shape></span>
0 0
- Android开发系列(十八):自定义控件样式在drawable文件夹下的XML实现
- Android 开发————自定义控件样式在drawable文件夹下的XML实现
- Android 之自定义控件样式在drawable文件夹下的XML实现
- Android 之自定义控件样式在drawable文件夹下的XML实现
- Android 自定义控件样式在drawable文件夹下的XML实现
- Android 之自定义控件样式在drawable文件夹下的XML实现
- Android 之自定义控件样式在drawable文件夹下的XML实现
- Android 之自定义控件样式在drawable文件夹下的XML实现
- Android 之自定义控件样式在drawable文件夹下的XML实现
- Android利用drawable文件夹自定义控件背景、样式
- Android利用drawable文件夹自定义控件背景、样式
- Android开发学习笔记:利用drawable中xml文件编写自定义控件样式(shape)
- 关于android中drawable文件夹下各类xml样式文件的使用详解
- 关于android中drawable文件夹下各类xml样式文件的使用详解
- 自定义图形在drawable文件夹下
- 【Android】自定义控件系列之TextView设置Drawable的大小
- Android drawable 样式的开发
- Android基础:drawable文件夹下 创建XML
- 和规范化股份和规范化
- 开发技巧,为了避免拖拽文件出现无法到达工程中,应该是件文件直接拷贝的工程中,不要在xcode里面操作
- 结构和几个号结构化
- 将工程中的文件写入到应用程序中得Documents文件下
- 10/25联通面试,移动笔试,毕设
- Android开发系列(十八):自定义控件样式在drawable文件夹下的XML实现
- 将图片设置为背景颜色
- 2014后半年计划
- 设置圆角
- leetcode - Median of Two Sorted Arrays
- JFreeChart 使用一 饼图之高级特性
- 计算内容的文字的size
- hdu 5073 Galaxy
- android开发环境搭建及环境变量的配置