Android 自定义 View (04自定义属性)

栏目: Android · 发布时间: 6年前

内容简介:怎么使用系统已经定死没有什么好挣扎的,按照步骤一步一步来即可。我们为上节定义的 View 添加一个颜色属性,改变绘制的圆圈的颜色。新建 attr.xml 文档,并定义属性。

怎么使用系统已经定死没有什么好挣扎的,按照步骤一步一步来即可。

  1. res/values 文件夹下创建 attr.xml 文件。

  2. attr.xml 文件中创建 declare-styleable 节点,并定义 name 属性。

    name 属性为自定义view类名。

  3. 然后就可以在 declare-styleable 节点中定义 attr 节点定义属性了,attr 节点有两个属性

    • name : 在布局中使用该属性的名字。
    • format : 属性的类型,一共有 10 种。
      • boolean : 布尔类型
      • color:颜色类型,可以是16进制表示也可以是@color表示
      • dimension:表示 attr 取值是尺寸类型,例如16sp,16dp,也可以是一个@dimen类型
      • float: 表示 attr 取值类型是整型或浮点型
      • integer:表示attr取值类型是整型
      • fraction:表是attr取值是百分之数类型,只能以%结尾。
      • string:表示 string 类型,或者指向 String 资源的 id。
      • refrerence 表示 attr 取值只能是指向一个资源的id。
      • enum 表示 attr 取值只能是枚举类型。
      • flag 表示 attr 取值是 flag 类型.
  4. 最后在自定义 view 的构造方法中获取自定义属性并进行解析。

使用

我们为上节定义的 View 添加一个颜色属性,改变绘制的圆圈的颜色。

step 1

新建 attr.xml 文档,并定义属性。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FreeStyleView">
        <attr name="color" format="color"></attr>
    </declare-styleable>
</resources>
复制代码

step 2

在自定义 View 中处理自定义属性。

这里将之前写的 init() 方法稍作改造。

public FreeStyleView(Context context) {
        super(context);
        init(context,null);
    }

    public FreeStyleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public FreeStyleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs) {
        // 用代码新建的没有xml定义的属性
        if (attrs != null){
            // 处理属性(Android系统自带的属性我们不用定义即可使用,but还是要处理的)
            // 这一步是解析属性,因为不这样操作通过循环我们也能拿到属性名字以及对应的值,但适配、以及获取资源不太易操作。
            TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.FreeStyleView);
            // 获取颜色,第一个参数是在 attr 定义的属性,系统做了处理,名称变成了FreeStyleView_color,
            // 第二个参数是默认值
            color = typedArray.getColor(R.styleable.FreeStyleView_color,color);
        }

        paint = new Paint();
        // 设置画笔模式,FILL 填充,STROKE 描边
        paint.setStyle(Paint.Style.STROKE);
        // 设置画笔颜色
        paint.setColor(color);
        // 设置画笔宽度,px 为单位,实际需要转换成 dp 值
        paint.setStrokeWidth(8);
    }
复制代码

step 3

最后在布局文件中使用自定义的属性

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.myapplication.view.FreeStyleView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:color="#eeff16"/>

</RelativeLayout>
复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

ActionScript 3.0 Cookbook中文版

ActionScript 3.0 Cookbook中文版

Joey Lott、Adobe Dev Library / 陈建勋 / 电子工业 / 2007-11-01 / 78.00元

本书讨论了在Flash Player里执行的ActionScript3.0语言,采用问题—解法—讨论的形式讲解开发过程中常见问题的实际解法,例如:检测用户的 Flash Player 版本或操作系统;格式化日期和货币类型;接受用户输入及操作文字字符串;在运行时绘制各种形状;访问音频和视频;使用 Flash Remoting 进行远程过程调用;加载、发送和检索 XML 数据等。 全书涵盖客户端......一起来看看 《ActionScript 3.0 Cookbook中文版》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具