GithubHelp home page GithubHelp logo

customview_understand's Introduction

CustomView_Understand

android中自定义属性的理解 自定义属性的步骤:

一,自定义一个CustomView(extends View)类
二,编写values/attrs.xml,在其中编写attr,declare-styleable,item等标签元素
三,在布局文件中使用自定义属性(这里要注意namespace问题,如果declare-styleable的name值为自定义的类名,相对来说会省事一些)
四,在CustomView的构造方法中中通过TypedArray获取自定义的属性

TypedArray的作用?
一般我们会使用TypedArray来获取布局文件中的自定义属性的值,其实通过构造方法中的AttributeSet参数我们也是可以获取到自定义属性的值的,但是当自定义属性的值是 引用类型(如:@dimen/dp100)时,使用AttributeSet参数获取到的值会是@+数字的字符串,而TypedArray则可以帮助我们获取到我们真正想要的数值.
博文中是这样写的:

TypedArray其实是用来简化我们的工作的,比如上例,如果布局中的属性的值是引用类型(比如:@dimen/dp100),如果使用AttributeSet去获得最终的像素值,
那么需要第一步拿到id,第二步再去解析id。而TypedArray正是帮我们简化了这个过程。

declare-styleable的作用?
博文中是这样写的:

styleale的出现系统可以为我们完成很多常量(int[]数组,下标常量)等的编写,简化我们的开发工作(想想如果一堆属性,自己编写常量,你得写成什么样的代码)。 那么大家肯定还知道declare-styleable的name属性,一般情况下写的都是我们自定义View的类名。主要为了直观的表达,该declare-styleable的属性, 都是该View所用的。

总结:

attrs.xml里面的declare-styleable以及item,android会根据其在R.java中生成一些常量方便我们使用(aapt干的),本质上, 我们可以不声明declare-styleable仅仅声明所需的属性即可。 我们在View的构造方法中,可以通过AttributeSet去获得自定义属性的值,但是比较麻烦,而TypedArray可以很方便的便于我们去获取。 我们在自定义View的时候,可以使用系统已经定义的属性。

customview_understand's People

Contributors

liuhuan2015 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.