Android之创建自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="customView">
<attr name="android:textColor"/>//在自定义属性中使用Android自带的属性名字
<attr name="customtextSize" format="dimension"/>//自定义属性,format属性表示该属性的单位
</declare-styleable>
</resources>
二、 我们在customView.java 代码修改如下,其中下面的构造方法是重点,我们获取定义的属性R.sytleable.customView_android_textColor和R.sytleable.customView_customtextSize, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.customView_customtextSize , 36 ); ), 防止我们在xml 文件中没有定义.从而使用默认值!
获取,customView 就是定义在<declare-styleable name="customView "></declare-styleable> 里的 名字,获取里面属性用 名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View; public class customView extends View{
private Paint mPaint;
private static final String mString = "Welcome to Mr Wei's blog";
public customView(Context context) {
super(context);
mPaint = new Paint();
// TODO Auto-generated constructor stub
}
public customView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.customView);
int textColor = a.getColor(R.styleable.customView_android_textColor, 0xff0000);
float textSize = a.getDimension(R.styleable.customView_customtextSize, 36);
mPaint.setColor(textColor);
mPaint.setTextSize(textSize);
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//设置填充
mPaint.setStyle(Style.FILL);
//画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
canvas.drawRect(new Rect(10, 10, 200, 200), mPaint);
mPaint.setColor(Color.BLUE);
//绘制文字
canvas.drawText(mString, 10, 110, mPaint);
}
}
三、将我们自定义的customView加入布局main.xml 文件中,平且使用自定义属性,自定义属性必须加上:
xmlns:test ="http://schemas.android.com/apk/res/com.lee0000.AutoCustomAttr "蓝色 是自定义属性的前缀,红色 是我们包名.main.xml 全部代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.lee0000.AutoCustomAttr"
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:text="@string/hello" />
<com.lee0000.AutoCustomAttr.customView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
test:customtextSize="20dp"//自定义属性
android:textColor="#fff">
</com.lee0000.AutoCustomAttr.customView>
</LinearLayout>
上一个demo下载:CustomAttrExample.zip
Android之创建自定义属性的更多相关文章
- Android自定义控件及自定义属性
Android自定义控件及自定义属性 自定义控件 创建自定义控件 自定义一个类,继承View 继承View还是哪个类,取决于你要实现一个什么样的控件 如果你要实现的是一个线性布局的组合控件,就可以继承 ...
- Android自定义控件之自定义属性
前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...
- Android 数据库管理— — —创建数据库
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...
- [转]Android Studio创建Xposed模块项目时BridgeApi的正确添加方式
使用Android Studio创建的空项目作为Xposed Module App,对于Api Jar包的引用方式,一开始是按照傻瓜式Jar Lib的处理方式,复制XposedBridgeApi-54 ...
- 【Android Studio使用教程2】Android Studio创建项目
创建项目 首先,先指出Android Studio中的两个概念. Project 和 Module .在Android Studio中, Project 的真实含义是工作空间, Module 为一个具 ...
- Android Studio创建项目
创建项目 首先,先指出Android Studio中的两个概念. Project 和 Module .在Android Studio中, Project 的真实含义是工作空间, Module 为一个具 ...
- Android中的自定义属性的实现
Android开发中,如果系统提供的View组件不能满足我们的需求,我们就需要自定义自己的View,此时我们会想可不可以为自定义的View定义属性呢?答案是肯定的.我们可以定义自己的属性,然后像系统属 ...
- Android Studio创建库项目及引用
Android Studio创建库项目其实创建的是在主项目下创建Module模块,这个Module模块创建的时候选择库项目模式. 为什么要这样处理呢?因为在Android Studio中一个WorkS ...
- android 自己创建一个凝视模板
android 自己创建一个凝视模板 作为一名程序猿 不仅要有一个写代码的能力,养成一个良好的编写习惯也是非常重要的. 今天给大家具体介绍一下怎样创建凝视模板,给每一个类和方法都自己手动去凝视信息也 ...
随机推荐
- java 中文转换成Unicode编码和Unicode编码转换成中文
转自:一叶飘舟 http://blog.csdn.net/jdsjlzx/article/details/ package lia.meetlucene; import java.io.IOExcep ...
- 2. MySQL
安装: apt-get install mysql-server 配置: mysql_install_db mysql_secure_installation 另有两处需要修改 [mysqld ...
- Java表单类双击提交
双击制御 有些时候一些操作会非常的耗费时间(Long Lived Operation),例如这个数据库的导出,表表生成等.有些时候程序的使用者看到很长时间服务器 没有反应,倾向于多次点击提交按钮.这样 ...
- HDU 1176 免费馅饼(记忆化搜索)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- Sqlmap注入技巧集锦
当我们注射的时候,判断注入 http://site/script?id=10 http://site/script?id=11-1 # 相当于 id=10 http://site/script?id= ...
- JQuery文件上传插件uploadify在MVC中Session丢失的解决方案
<script type="text/javascript"> var auth = "@(Request.Cookies[FormsAuthenticati ...
- [ZZ] Deferred Rendering and HDR
http://www.gamedev.net/topic/496785-deferred-rendering-and-hdr/ Quote: Original post by jstrohYeah I ...
- php获取某年某月的天数 【转】
function days_in_month($month, $year) { // calculate number of days in a month return $month == 2 ? ...
- Linux下使用inode删除文件
Linux 下有时候某些文件无法使用 rm 直接删除, 比如该文件的文件名含有终端不能正确显示的字符.# ls -litotal 0441511 -rw-r--r-- 1 root root 0 Ap ...
- P1091 合唱队形
水题 #include <bits/stdc++.h> using namespace std; const int maxn = 105; int main(int argc, char ...