Android开发系列(十八):自己定义控件样式在drawable目录下的XML实现
2023-09-14 09:08:16 时间
在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>
相关文章
- android.view.inflateexception binary xml file line 异常的解决方法
- unity Android 打包后读取 xml 文件
- C# Xamarin For Android自动升级项目实战
- Eclipse导入android项目包xml报错未生成R文件
- Android网络之数据解析----SAX方式解析XML数据
- android程序崩溃后重启
- linux 程序移植到Android
- (转载)ANDROID STRINGS.XML 中的特殊字符转义
- 《Android进阶之光》--网络编程与网络框架
- bug_ _ android.view.InflateException: Binary XML file line #2: Error inflating class <unknown
- android xml文件中出现如下提醒:This tag and its children can be replaced by one <TextView/> and a compound drawable
- android webview处理下载内容
- 通过在xml布局文件中设置android:onClick=""来实现组件单击事件
- Android--使用开源vitamio做万能视频播放器
- Android Studio - xml布局文件不显示代码怎么办?
- android Manifest.xml选项
- Android 项目经验汇总:Fiddler的坑,证书无效
- Android之aaapt查看apk中AndroidManifest.xml(一百三十四)
- Android项目包装apk和apk反编译,xml反编译
- AndroidManifest.xml中android:configChanges的简介
- android解析XML总结(SAX、Pull、Dom三种方式) <转载>