Android 自定义属性

引言

Android 自定义属性想必大家都不陌生,参照官方的guide中的步骤

  • Define custom attributes for your view in a < declare-styleable> resource element
    自定义一个CustomView(extends View )
  • Specify values for the attributes in your XML layout
    编写values/attrs.xml,在其中编写styleable和item等元素
  • Retrieve attribute values at runtime
    在布局文件中CustomView使用自定义的属性(注意namespace)
  • Apply the retrieved attribute values to your view
    在CustomView的构造方法中通过TypedArray获取

但,同时也要遵循如下原则

  • Conform to Android standards
  • Provide custom styleable attributes that work with Android XML layouts
  • Send accessibility events
  • Be compatible with multiple Android platforms.

自定义属性是如何生效的

常见的自定义属性的例子

Adroid Studio中自定义属性,是很简单的,这里以自定义view属性为例。如下gif图片所示
自定义view
在自定义的MyView之后,AS自动生成了如下文件

  • sample_my_view.xml
    这个主要是使用了我们自定义view的一个布局文件。
  • attrs_my_view.xml
    我们自定义的一些属性所在的xml文件
  • MyView.java
    我们自定义view的java实现

自定义view属性中文件分析

生成的布局文件分析

所生成的布局文件源码如下所示

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/cp.com.myview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <cp.com.myview.MyView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="#ccc"
        android:paddingBottom="40dp"
        android:paddingLeft="20dp"
        app:exampleColor="#33b5e5"
        app:exampleDimension="24sp"
        app:exampleDrawable="@android:drawable/ic_menu_add"
        app:exampleString="Hello, MyView" />
</FrameLayout>

这里需要注意几点

  • 命名空间
    xmlns:app=”http://schemas.android.com/apk/res/cp.com.myview”
    其中 xmlns就是xml namespace的意思啦。后面的值需要注意的一点,要跟上你的应用程序包名。=”http://schemas.android.com/apk/res/[your package name]”
  • 使用我们自定义的view组件
    cp.com.myview.MyView,如我们刚才自定义的名字空间为app,那么我们在当前MyView标签下,就可以使用app来针对我们所声明的自定义属性进行赋值了。
  • 声明自定义view的自定义属性
    我们在 attrs_my_view.xml中声明了自定义MyView的如下属性

    • exampleString ,类型为 string
    • exampleDimension,类型为dimension
    • exampleColor,类型为color
      当然我们可以定义更多的属性,详细可以参考custom-views_create-view.
    <resources>
    <declare-styleable name="MyView">
        <attr name="exampleString" format="string" />
        <attr name="exampleDimension" format="dimension" />
        <attr name="exampleColor" format="color" />
        <attr name="exampleDrawable" format="color|reference" />
    </declare-styleable>
    </resources>

生成的属性文件

<resources>
    <declare-styleable name="MyView">
        <attr name="exampleString" format="string" />
        <attr name="exampleDimension" format="dimension" />
        <attr name="exampleColor" format="color" />
        <attr name="exampleDrawable" format="color|reference" />
    </declare-styleable>
</resources>

自定义属性使用

参考资料

在这里班门弄斧了,详细的可以参考大神的博客,机器Google官方文档
Android 深入理解Android中的自定义属性

http://developer.android.com/intl/zh-cn/training/custom-views/create-view.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值