Android使用AttributeSet自定义控件的方法

简介: 在xml 文件里定义控件的属性,我们已经习惯了android:attrs="" ,那么我们能不能定义自己的属性能,比如:test:attrs="" 呢?答案是必须能啊.

xml 文件里定义控件的属性,我们已经习惯了android:attrs="" ,那么我们能不能定义自己的属性能,比如:test:attrs="" 呢?答案是必须能啊.

下面就来演示一下粗略步骤:##

<ol><li> 在res/values 文件下定义一个attrs.xml 文件.代码如下:

<?xml version="1.0" encoding="utf-8"?>  
<resources>
  <declare-styleable name="DemoView">  
  <attr name="textColor" format="color" />  
  <attr name="textSize" format="dimension" />  
</declare-styleable>  
</resources>

<li>我们在DemoView.java 代码修改如下,其中下面的构造方法是重点,我们获取定义的属性我们R.sytleable.DemoView_textColor, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.DemoView_textSize, 36 ); ) 防止我们在xml 文件中没有定义.从而使用默认值!
获取,DemoView 就是定义在<declare-styleable name="DemoView "></declare-styleable> 里的 名字,获取里面属性用** 名字_ 属性** 连接起来就可以.TypedArray 通常最后调用** .recycle()** 方法,为了保持以后使用该属性一致性!

public DemoView(Context context,AttributeSet attrs)  
    {  
        super(context,attrs);  
        mPaint = new Paint();  
          
        TypedArray a = context.obtainStyledAttributes(attrs,  
                R.styleable.DemoView);  
          
        int textColor = a.getColor(R.styleable.DemoView_textColor,  
                0XFFFFFFFF);  
        float textSize = a.getDimension(R.styleable.DemoView_textSize, 36);  
          
        mPaint.setTextSize(textSize);  
        mPaint.setColor(textColor);  
          
        a.recycle();  
    }  

DemoView.java 全部代码如下:

package com.android.demo;  
import android.content.Context;  
import android.content.res.TypedArray;  
import android.graphics.Canvas;  
import android.graphics.Color;  
import android.graphics.Paint;  
import android.graphics.Rect;  
import android.graphics.Paint.Style;  
import android.util.AttributeSet;  
import android.view.View;  
public class DemoView extends View {  
    private Paint mPaint;  
    private Context mContext;  
    private static final String mString = "Welcome to Mr Samson's blog";  
      
    public DemoView (Context context) {  
        super(context);  
        mPaint = new Paint();  
    }  
    public DemoView (Context context,AttributeSet attrs)  
    {  
        super(context,attrs);  
        mPaint = new Paint();  
          
        TypedArray a = context.obtainStyledAttributes(attrs,  
                R.styleable.MyView);  
          
        int textColor = a.getColor(R.styleable.DemoView_textColor,  
                0XFFFFFFFF);  
        float textSize = a.getDimension(R.styleable.DemoView_textSize, 36);  
          
        mPaint.setTextSize(textSize);  
        mPaint.setColor(textColor);  
          
        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, 100, 100), mPaint);  
          
        mPaint.setColor(Color.BLUE);  
        //绘制文字  
        canvas.drawText(mString, 10, 110, mPaint);  
    }  
}  

<li>将我们自定义的DemoView 加入布局main.xml 文件中,平且使用自定义属性,自定义属性必须加上:
**xmlns:test ="http://schemas.android.com/apk/res/com.android.demo "蓝色 是自定义属性的前缀,红色 **是我们包名.
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.android.demo"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  >  
  <TextView    
      android:layout_width="fill_parent"   
      android:layout_height="wrap_content"   
      android:text="@string/hello" />  
  <com.android.demo.DemoView  
      android:layout_width="fill_parent"   
      android:layout_height="fill_parent"   
      test:textSize="18px"  
      test:textColor="#ffffff"/>  
</LinearLayout>  
目录
相关文章
|
2月前
|
存储 Shell Android开发
基于Android P,自定义Android开机动画的方法
本文详细介绍了基于Android P系统自定义开机动画的步骤,包括动画文件结构、脚本编写、ZIP打包方法以及如何将自定义动画集成到AOSP源码中。
47 2
基于Android P,自定义Android开机动画的方法
|
2月前
|
Android开发
基于android-11.0.0_r39,系统应用的手动签名方法和过程
本文介绍了基于Android 11.0.0_r39版本进行系统应用手动签名的方法和解决签名过程中遇到的错误,包括处理`no conscrypt_openjdk_jni-linux-x86_64`和`RegisterNatives failed`的问题。
91 2
|
7天前
|
缓存 前端开发 Android开发
安卓应用开发中的自定义控件
【9月更文挑战第28天】在安卓应用开发中,自定义控件是提升用户界面和交互体验的关键。本文通过介绍如何从零开始构建一个自定义控件,旨在帮助开发者理解并掌握自定义控件的创建过程。内容将涵盖设计思路、实现方法以及性能优化,确保开发者能够有效地集成或扩展现有控件功能,打造独特且高效的用户界面。
|
13天前
|
ARouter 测试技术 API
Android经典面试题之组件化原理、优缺点、实现方法?
本文介绍了组件化在Android开发中的应用,详细阐述了其原理、优缺点及实现方式,包括模块化、接口编程、依赖注入、路由机制等内容,并提供了具体代码示例。
30 2
|
1月前
|
搜索推荐 Android开发 开发者
探索安卓开发中的自定义控件
【9月更文挑战第5天】在安卓开发的海洋中,自定义控件如同一艘精致的小船,让开发者能够乘风破浪,创造出既独特又高效的用户界面。本文将带你领略自定义控件的魅力,从基础概念到实战应用,一步步深入理解并掌握这一技术。
|
2月前
|
Android开发
Android在rootdir根目录创建自定义目录和挂载点的方法
本文介绍了在Android高通平台的根目录下创建自定义目录和挂载点的方法,通过修改Android.mk文件并使用`LOCAL_POST_INSTALL_CMD`变量在编译过程中添加目录,最终在ramdisk.img的系统根路径下成功创建了`/factory/bin`目录。
85 1
|
2月前
|
开发工具 uml git
AOSP源码下载方法,解决repo sync错误:android-13.0.0_r82
本文分享了下载AOSP源码的方法,包括如何使用repo工具和处理常见的repo sync错误,以及配置Python环境以确保顺利同步特定版本的AOSP代码。
169 0
AOSP源码下载方法,解决repo sync错误:android-13.0.0_r82
|
2月前
|
Android开发 UED 开发者
安卓开发中的自定义控件基础
【8月更文挑战第31天】在安卓应用开发过程中,自定义控件是提升用户界面和用户体验的重要手段。本文将通过一个简易的自定义按钮控件示例,介绍如何在安卓中创建和使用自定义控件,包括控件的绘制、事件处理以及与布局的集成。文章旨在帮助初学者理解自定义控件的基本概念,并能够动手实践,为进一步探索安卓UI开发打下坚实的基础。
|
2月前
|
存储 缓存 前端开发
安卓开发中的自定义控件实现及优化策略
【8月更文挑战第31天】在安卓应用的界面设计中,自定义控件是提升用户体验和实现特定功能的关键。本文将引导你理解自定义控件的核心概念,并逐步展示如何创建一个简单的自定义控件,同时分享一些性能优化的技巧。无论你是初学者还是有一定经验的开发者,这篇文章都会让你对自定义控件有更深的认识和应用。
|
2月前
|
Android开发
Android项目架构设计问题之onFirstItemVisibleChanged方法的调用如何解决
Android项目架构设计问题之onFirstItemVisibleChanged方法的调用如何解决
30 0
下一篇
无影云桌面