自定义View基础之自定义属性和构造函数

自定义View分类

  • 继承自View
    通常用于实现一些不规则的效果,这些效果不方便或者不能够用布局组合的方式实现。这种自定义的View需要通过自定义绘制方式来实现,即重写onDraw方法,需要注意的是,注意wrap_content和padding两个属性的处理。不做处理的话会导致这两个参数失效
  • 继承自ViewGroup
    主要用于实现自定义的布局,比如将几个不同的View合并起来作为一个新的常用的Layout,使用的时候直接引入就会方便许多。需要注意的是,ViewGroup和各个子元素的的Measure,Layout过程一定要处理好。
  • 继承自特定的View
    这种方法比较常见,主要用于对现有View进行扩展,实现自己需要的特殊功能,而且实现起来相对简单,wrap_content和padding也不需要手动处理
  • 继承自特定ViewGroup
    这种方法也比较常见,而且不需要手动处理ViewGroup的Measure,Layout过程,通常来说,继承自ViewGroup能够实现的布局采用这种方式都能实现,只不过直接继承自ViewGroup更接近底层,灵活性也更好。

自定义View流程及常用方法解析

1.构造函数

View的构造函数有四种重载:

public void MyView(Context context) {}
public void MyView(Context context, AttributeSet attrs) {}
public void MyView(Context context, AttributeSet attrs, int defStyleAttr) {}
public void MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {}

常用到的只有第一第二两种。第一个只有Context参数,在代码中创建View时,会调用此方法。
第二个有两个参数,Context和AttributeSet,用在XML中创建View,AttributeSet用来保存XML文件中View的各项属性。
后两个参数的含义:
defStyleAttr参数:这是一个定义在attrs.xml文件中的attribute。这个值起作用需要两个条件:1. 值不为0;2. 在Theme中使用了(出现即可)。
defStyleRes参数: 这是在styles.xml文件中定义的一个style。只有当defStyleAttr没有起作用,才会使用到这个值。
在分析这两个参数的含义之前,我们先来了解一下View的属性是怎样设置的.

View的Style和Theme

首先,在XML文件中为View指定属性有以下几种方式:

  • 直接在layout中设置
  • 设置style,并在layout中指定Style
  • Application和Activity可以指定theme,在theme中设置当前Application或者Activity的属性默认值。
        <EditText
        android:id="@+id/cusview_attrs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        style="@style/MyTextStyle"
        android:text="simple image view"
        />

这是一个EditText,我们在layout文件中设置了他的基本属性,android:textSize="24sp",这个很容易理解。除此之外还有一个style="@style/MyTextStyle",这个Style是在values/style.xml文件中定义的:

        <style name="MyTextStyle" >
        <item name="android:textColor" >#00FF00</item>
        </style>

指定了一个颜色为绿色,可以看一下效果,最终呈现出来的属性就是24sp+绿色。
那么又有新的问题了,如果同时在属性和Style中对同一属性设置不同的值,结果会是怎样的? 大家可以试一下,layout中直接指定的属性优先级高于Style。
关于Style,他还有一个parent父属性,指定当前Style是继承自哪一个Style的:

        <style name="MyTextStyle" parent="@android:style/TextAppearence">
        <item name="android:textColor" >#00FF00</item>
        </style>

这表示我们自定义的MyTextStyle继承了android:style/TextAppearence的默认属性,同时指定textColor覆写了父属性。

介绍完了Style,现在我们再来看一下Theme是怎样使用的。Style通常是针对某一个View进行设置的,而theme针对的是Application和Activity。打开manifest文件,我们会看到以下的内容:

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
......

Application会指定一个theme属性,他的值在style中定义了:

 <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

是完全继承自AppBaseTheme的。就是说,这个应用的主题就是AppBaseTheme。当然我们也可以自定义theme。

    <style name="MyAppTheme" parent="AppBaseTheme">
        <item name="android:background">#00FF00</item>
    </style>

这里设置了一个自定义的主题,背景色为绿色,将它应用的某一个Activity:

<activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
            android:configChanges="keyboardHidden|screenSize"
            android:theme="@style/MyAppTheme">
......

然后再运行一下,看看效果你就会发现,被指定的Activity整个都绿了。。。。
Android系统的theme.xml和style.xml存放位置在sdk\platforms\android-x\data\res\values下,感兴趣的可以自己查看。

构造函数的参数分析

我们看一下View的构造函数源码:

final TypedArray a = context.obtainStyledAttributes(
                attrs, com.android.internal.R.styleable.View, defStyleAttr, defStyleRes);

View的各项属性是context.obtainStyledAttributes方法来获取的,他包含四个参数
第一,三,四个参数很明显,是从构造函数里传入的,第二个参数com.android.internal.R.styleable.View,表名了我们将要获取的属性。我们用一个例子来解释,顺便学习一下自定义参数的使用。
第一步,在values下新建一个attrs.xml文件,这里定义好所需要的参数名称和格式:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyCustomizeView">
        <attr name="attr_1" format="string"/>
        <attr name="attr_2" format="string"/>
        <attr name="attr_3" format="string"/>
        <attr name="attr_4" format="string"/>
        <attr name="attr_5" format="string"/>
    </declare-styleable>
<attr name="CustomizeStyle" format="reference"/>
</resources>

这里我们定义了一个名称为 MyCustomizeView 的styleable,他包含四项属性 attr_1,2 , 3, 4, 5,另外我们还定义了一个属性CustomizeStyle,他的类型是reference,表示它可以接收一个style引用,各项属性的值,可以在这个引用中定义。

将属性定义在styleable 中,和直接定义没有很大的区别,不管定义在哪个位置,这些attribute都会生效,只是定义在styleable 中的styleable ,系统会在R.styleable中生成相关属性。我们可以通过R.styleable.MyCustomizeView来引用他。 直接定义的,需要通过R.attr.CustomizeStyle来引用。
各项属性的使用方法如下:
在layout文件中使用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myattrs="http://schemas.android.com/apk/com.training"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
        <com.training.view.MyCustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        myattrs:attr_1="Attr 1 set in layout "
        style="@style/MyStyle"
       />
</LinearLayout>

注意,要使用自定义属性,必须新建一个自定义属性的命名空间:

xmlns:名字="http://schemas.android.com/apk/res/包名"
或者xmlns:名字="http://schemas.android.com/apk/res-auto"

我们在属性中使用了style,所以也要在style.xml中定义它:

     <style name="MyStyle">
        <item name="attr_1">attr 1 set in style </item>
        <item name="attr_2">attr 2 set in style </item>
    </style>

现在我们回到构造函数,

public void MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {}

后边两个参数怎样使用呢? 开始的部分我们提到了:
defStyleAttr参数:这是一个定义在attrs.xml文件中的attribute。这个值起作用需要两个条件:1. 值不为0;2. 在Theme中使用了(出现即可)。
defStyleRes参数: 这是在styles.xml文件中定义的一个style。只有当defStyleAttr没有起作用,才会使用到这个值。
defStyleAttr,是定义在attrs.xml中的,在attrs中我们已经定义了:

<attr name="CustomizeStyle" format="reference"/>

还要在Theme使用它。代码如下:

    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="attr_1">attr 1 set in theme</item>
        <item name="attr_2">attr 2 set in theme</item>
        <item name="attr_3">attr 3 set in theme</item>
        <item name="CustomizeStyle">@style/StyleInTheme</item>
    </style>
    <style name="StyleInTheme">
        <item name="attr_1">attr 1 set in theme reference</item>
        <item name="attr_2">attr 2 set in theme reference</item>
        <item name="attr_3">attr 3 set in theme reference</item>
        <item name="attr_4">attr 4 set in theme reference</item>
    </style>

除此之外,我们再定义一个style,对应defStyleRes参数。

     <style name="DefaultCustomizeStyle">
    <item name="attr_1">attr 1 set in default style res</item>
    <item name="attr_2">attr 2 set in default style res</item>
    <item name="attr_3">attr 3 set in default style res</item>
    <item name="attr_4">attr 4 set in default style res</item>
    <item name="attr_5">attr 5 set in default style res</item>
    </style>

现在准备工作已经结束,我们做了以下工作:
1:定义了5个自定义属性,attr_1,2,3,4,它们接收String值,还有一个CustomizeStyle属性,接收一个reference
2:在layout文件中直接给attr_1赋值了,另外还设置了一个MyStyle,在style中给attr_1,2都进行了赋值
3:在Application使用的theme中,直接对attr_1,2,3进行赋值,同时设置了CustomizeStyle的引用对象,在引用中对attr_1,2,3,4赋值。
4:设置了一个DefaultCustomizeStyle,对attr_1,2,3,4,5都进行赋值。
现在我们看一下怎样使用这些参数:

public class MyCustomView extends View{
    private final String TAG = "ViewConstructor";
    public MyCustomView(Context context) {
        super(context);
    }
    public MyCustomView(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.CustomizeStyle);
    }
    public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, R.style.DefaultCustomizeStyle):      
    }
    public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr,
            int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomizeView, defStyleAttr, defStyleRes);
        String one = a.getString(R.styleable.MyCustomizeView_attr_1);
        String two = a.getString(R.styleable.MyCustomizeView_attr_2);
        String three = a.getString(R.styleable.MyCustomizeView_attr_3);
        String four = a.getString(R.styleable.MyCustomizeView_attr_4);
        String five = a.getString(R.styleable.MyCustomizeView_attr_5);
        Log.d(TAG, "ATTR_1------>" + one);
        Log.d(TAG, "ATTR_2------>" + two);
        Log.d(TAG, "ATTR_3------>" + three);
        Log.d(TAG, "ATTR_4------>" + four);
        Log.d(TAG, "ATTR_5------>" + five);
        a.recycle();
    }

现在运行一下,就可以看到结果了:

ATTR_1------>Attr 1 set in layout
ATTR_2------>attr 2 set in style
ATTR_3------>attr 3 set in theme reference
ATTR_4------>attr 4 set in theme reference
ATTR_5------>null

attr_1在layout,style,theme,theme的属性引用,defaultStyle中都赋值了,结果是layout中的赋值
attr_2在style,theme,theme的属性引用,defaultStyle中赋值了,结果是style中的值
attr_3在theme,theme的属性引用,defaultStyle中赋值了,结果是theme的属性引用中的值
attr_4在theme的属性引用,defaultStyle中赋值了,结果是theme的属性引用中的值
attr_5在defaultStyle中赋值了,结果是null。

也就是说,他们的优先级是这样的:

直接在layout中定义>在style定义>defStyleAttr(在theme中,对属性的引用赋值,对应第三个参数)>defStyleRes(设置默认的style,对应第四个参数)。
同时也印证了上文提到的,defStyleRes参数只有当defStyleAttr没有起作用,才会使用到这个值。

如果将defStyleAttr设置为0:

    public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, 0, R.style.DefaultCustomizeStyle); 
    }

再运行一下,结果如下:

ATTR_1------>Attr 1 set in layout
ATTR_2------>attr 2 set in style
ATTR_3------>attr 3 set in default style res
ATTR_4------>attr 4 set in default style res
ATTR_5------>attr 5 set in default style res

这时候,第四个参数,defStyleRes终于起作用了。

关于自定义参数的详细说明,可以参考这位同学的文章

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容