android自定义view之自定义属性

简介: 这两天在Android中用到了自定义view,在自定义view时也顺便使用了下自定义属性。自定义属性以前只是耳闻 未曾谋面,这次借机会对自定义属性进行了一番学习,顺便总结了一下自定义属性的使用。

这两天在Android中用到了自定义view,在自定义view时也顺便使用了下自定义属性。自定义属性以前只是耳闻 未曾谋面,这次借机会对自定义属性进行了一番学习,顺便总结了一下自定义属性的使用。
下面扫盲班老司机要开车了,小白快刷卡上车,大神拒载。


img_75344b054c67b12b237be903e9bbcb8c.jpe
老司机

Android中经常用到自定义view,既然用了自定义view那就不得不提自定义属性。你是否思考过为什么我们在xml文件中进行布局时可以直接通过android:layout_width="match_parent"就可以设置控件的宽度呢?不只是宽度,几乎控件的所有属性都可以在xml文件中进行设置,这是怎样实现的呢,this is a question

img_3e94fed8f19b9f20a0cc5200e10a6c59.png
TextView部分属性

我们自定义view时能不能也像系统提供的控件一样在xml文件中设置属性呢。答案是当然可以了,用到的就是今天要说的自定义属性。废话不多说 直接开干。
1,首先在res 的values文件夹下新建一个attrs.xml文件,就是这样


img_f0e8aafa9740d2a7fb6a32f64410bd30.png
新建的attrs.xml文件

2,开始编写我们需要的属性。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="burce">
        <attr name="mHeight" format="integer"/>
        <attr name="mWidth" format="integer"/>
        <attr name="mName" format="string"/>
        <attr name="sex" format="enum">
            <enum name="man" value="0"/>
            <enum name="woman" value="1"/>
        </attr>
        <attr name="student" format="boolean"/>
    </declare-styleable>
</resources>

说一下用到的东西
<declare-styleable name="burce">其中的name的值随便定义一个,不要与系统的起冲突。
<attr name="mHeight" format="integer"/>name就是自定义的属性的名字(比如系统控件的android:layout_width) format 就是属性的类型,这里支持10种类型,常用的有string,integer,boolean等等,这次我们用到了整形,枚举和布尔
注意:我们在自定义属性的名字的时候不能与系统的名字冲突,否则会报错

3,新建一个类继承View类,实现3个构造方法,然后获取我们自定义的属性

public class MyView extends View {
    private static final String TAG = "MyView";
    private int heiget;
    private int width;
    private String name;
    private int sex;
    private boolean student;
    public MyView(Context context) {
        this(context,null);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.burce);
        heiget=array.getInt(R.styleable.burce_mHeight,0);
        width=array.getInt(R.styleable.burce_mWidth,0);
        name=array.getString(R.styleable.burce_mName);
        sex=array.getInt(R.styleable.burce_sex,0);
        student=array.getBoolean(R.styleable.burce_student,true);
        array.recycle();

        Log.i(TAG, "height: "+heiget);
        Log.i(TAG, "width: "+width);
        Log.i(TAG, "name: "+name);
        Log.i(TAG, "sex: "+sex);
        Log.i(TAG, "student: "+student);

    }
}

TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.burce);

img_b22840ce0b53402a7a5d5ece177cda52.png
Android Developers

这是Google官方给的解释,就简单说一下两个参数怎么填吧,第一个填形参的attrs,第二个填 R.styleable是固定写法,bruce是 <declare-styleable name="burce">中的name的值。
4,回到MainActivity的布局文件中使用我们的自定义view

<?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="com.myviewtest.MainActivity">
    <com.myviewtest.MyView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:mName="bruce"
        app:sex="man"
        app:mHeight="100"
        app:mWidth="100"
        app:student="true"/>
</RelativeLayout>

注意:如果Android studio没有加上命名空间的话需要自己加上xmlns:app="http://schemas.android.com/apk/res-auto" 只有声明了命名空间才能使用自定义属性,不懂啥是命名空间的同学呢自己Google学习一下吧(最近在学C++,我就安C++中的命名空间理解的,如果不正确还请大神赐教)
<attr name="height" format="integer"/> 这里的我们在这用了app命名空间,所有所有的自定义属性的开头都加上了“app:”。

准备工作都做好了接下来我们吧应用跑起来看看吧,这里我们通过打印log查看自定义属性的值。

img_1533f8662b53f1d94829f5456f1ec3c1.png
运行结果

通过log可以得知 我们在自定义view中成功的获取到了属性的值。好的老司机平安到站,小白有序下车。
由于你遇到了一个假的老司机,文章中如果有不正确的地方还请各位大神在评论区指正,老司机在这里抱拳了。

目录
相关文章
|
3天前
|
Android开发
Android面试题之自定义View注意事项
在Android开发中,自定义View主要分为四类:直接继承View重写onDraw,继承ViewGroup创建布局,扩展特定View如TextView,以及继承特定ViewGroup。实现时需注意:支持wrap_content通过onMeasure处理,支持padding需在onDraw或onMeasure/onLayout中处理。避免在View中使用Handler,使用post系列方法代替。记得在onDetachedFromWindow时停止线程和动画以防止内存泄漏。处理滑动嵌套时解决滑动冲突,并避免在onDraw中大量创建临时对象。
13 4
|
1天前
|
Android开发
Android面试题之View的invalidate方法和postInvalidate方法有什么区别
本文探讨了Android自定义View中`invalidate()`和`postInvalidate()`的区别。`invalidate()`在UI线程中刷新View,而`postInvalidate()`用于非UI线程,通过消息机制切换到UI线程执行`invalidate()`。源码分析显示,`postInvalidate()`最终调用`ViewRootImpl`的`dispatchInvalidateDelayed`,通过Handler发送消息到UI线程执行刷新。
11 1
|
6天前
|
前端开发 API Android开发
Android自定义View之Canvas一文搞定
这篇文章介绍了Android自定义View中如何使用Canvas和Paint来绘制图形。Canvas可理解为画布,用于绘制各种形状如文字、点、线、矩形、圆角矩形、圆和弧。常见API包括`drawText()`、`drawPoint()`、`drawLine()`、`drawRect()`等。文章还提到了Canvas的保存、恢复、平移和旋转方法,通过绘制钟表盘的例子展示了如何实际应用。总结关键点:Canvas与Paint结合用于图像绘制,掌握Canvas的基本绘图函数及坐标变换操作是自定义View的关键。
7 0
Android自定义View之Canvas一文搞定
|
6天前
|
消息中间件 前端开发 Android开发
Android面试题自定义View之Window、ViewRootImpl和View的三大流程
Android开发中,View的三大核心流程包括measure(测量)、layout(布局)和draw(绘制)。MeasureSpec类在测量过程中起到关键作用,它结合尺寸大小和模式(EXACTLY、AT_MOST、UNSPECIFIED)来指定View应如何测量。onMeasure方法用于自定义View的测量,布局阶段,ViewGroup调用onLayout确定子元素位置,而draw阶段按照特定顺序绘制背景、内容、子元素和装饰。整个流程始于ViewRootImpl的performTraversals,该方法触发测量、布局和绘制。
13 0
|
12天前
|
XML 数据格式
Android-自定义三角形评分控件
Android-自定义三角形评分控件
11 0
|
12天前
Android-自定义流布局标签
Android-自定义流布局标签
10 0
|
12天前
|
Android开发
Android自定义之高仿淘宝下拉刷新
Android自定义之高仿淘宝下拉刷新
13 0
|
12天前
|
Android开发
Android自定义之QQ身边的人
Android自定义之QQ身边的人
|
12天前
|
Android开发
Android自定义一个属于自己的刻度尺
Android自定义一个属于自己的刻度尺
11 0
|
Android开发
Android Studio 自定义设置注释模板
Android Studio 自定义设置注释模板
416 0
Android Studio 自定义设置注释模板