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

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

内容简介:怎么使用系统已经定死没有什么好挣扎的,按照步骤一步一步来即可。我们为上节定义的 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>
复制代码

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

查看所有标签

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

Implementing Responsive Design

Implementing Responsive Design

Tim Kadlec / New Riders / 2012-7-31 / GBP 27.99

New devices and platforms emerge daily. Browsers iterate at a remarkable pace. Faced with this volatile landscape we can either struggle for control or we can embrace the inherent flexibility of the w......一起来看看 《Implementing Responsive Design》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具