Android之创建自定义属性

一、 在res/values 文件下定义一个attrs.xml 文件.代码如下:

<?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

时间: 2024-08-29 07:05:51

Android之创建自定义属性的相关文章

Android中的自定义属性的实现

Android开发中,如果系统提供的View组件不能满足我们的需求,我们就需要自定义自己的View,此时我们会想可不可以为自定义的View定义属性呢?答案是肯定的.我们可以定义自己的属性,然后像系统属性一样用在layout布局中. 通过下面3步既可以完成自定义属性: 第一步:在values文件夹下的attrs.xml文件(如果没有可以收到建立)中定义属性资源文件 1 <?xml version="1.0" encoding="utf-8"?> 2 <

《android 1: 创建一个安卓项目》

创建方式有两种: 通过Eclipse创建 在工具栏上选择New>android>android application project,或者在导航栏上选择file>new>project>android>android application project. 在出现的窗口上填上与选择你的 Application Name .Project Name.Package Name.Minimum Required SDK.Target SDK.Compile With.Th

[Android]如何创建一个View的分割线

如何创建一个View的分割线,如上图 我们见介绍三种可以创建看起来很不错的view的分割线,如在button之间添加分割线. 这个例子是将为LinearLayout内的三个Button间添加分割线. 这三个例子可能容易实现,相信会有更好的实现办法. 1 人工添加LinearLayout的分割线 我们可以创建一个View,这个View就是分割线,只要简单在Button之间添加这个分割线就可以. 分割线的实现,如下: <View android:layout_height="fill_pare

android下创建文件夹和修改其权限的方法

原文:http://www.cnblogs.com/wanqieddy/archive/2011/12/28/2304906.html 由于工作的需要,今天研究了在android下创建文件夹和修改其权限的方法,需要了解的是每个应用程序包都会有一个私有的存储数据的目录(类似文件夹),只有属于该包的应用程序才能写入该目录空间,每个包应用程序的私有数据目录位 于Android绝对路径/data/data/<包名>/目录中.除了私有数据目录应用程序还拥有/sdcard目录(即SD Card的写入权限,

cceditbox和cocostudio联合界面点击事件无响应问题,Android中创建文件夹问题

这两个问题折腾了我一天多时间,尽快下班时候把创建文件夹问题给解决了,刚把cceditbox点击事件无响应问题解决了.真是大爽. 1.创建文件夹 _mkdir()这个c函数就行,在vs ide中,定位的头文件是direct.h,但是这个文件不被ndk编译通过,没有这个文件.好吧,死脑筋,开始怀疑eclipse库路径设置有问题,但是不知道哪不对,搜索到了头大地步,好吧,认真仔细一看,原来是在ndk中,_mkdir函数放在了#include <sys/stat.h>中,改为这个头文件就行了. 2.点

[Android UI] ActionBar 自定义属性

actionbar 默认放在顶部, 如果在application或者activity中加入 android:uiOptions="splitActionBarWhenNarrow" 那么,actionbar将在底部显示. 自定义属性,包括自定义actionbar的背景颜色 <!-- the theme applied to the application or activity --> <style name="CustomActivityTheme&quo

Android Studio创建Android Library发布至本地仓库

本文通过Android Studio创建一个Android Library项目,然后发布到本地的Maven仓库. 1.创建Android Library模块 2.配置build.gradle apply plugin: 'com.android.library' apply plugin: 'maven' android {     compileSdkVersion 21     buildToolsVersion "21.1.2"     defaultConfig {      

Android Studio创建项目

创建项目 首先,先指出Android Studio中的两个概念. Project 和 Module .在Android Studio中, Project 的真实含义是工作空间, Module 为一个具体的项目. 在 Eclipse 中,我们可以同时对多个 Eclipse 的 Project 进行同时编辑,这些 Project 在同一个 workspace 之中.在Android Studio中,我们可以同时对多个Android Studio的 Module 进行同时编辑,这些 Module 在同

android studio创建模拟器报错解决 emulator: ERROR: This AVD&#39;s configuration is missing a kernel file!!

android studio创建模拟器报错 emulator: ERROR: This AVD's configuration is missing a kernel file!! 的解决办法 原因有二:1 没有,解决办法通过sdk mangager 下载 2 找不到,解决办法看系统环境变量path(此种情况多数发生在android Studio和EclipseADT同时存在的情况下) 原因是在刚刚安装完毕的androidstudio的sdk目录下没有system-image目录,也许有但没有相