[进行中]自定义view 1,仿TextView

前言
  • 参考了这篇文章,先照着敲打一遍,然后用自己的思路实现,再整理到博客里,但是没有原作者写得好,推荐去看原作者的,这里仅为我加深理解,仅实现了仿textview显示文字,不涉及其他。
  • 写完后,会去跟作者的博客比较,然后修改内容,就会有雷同,有抄袭的嫌疑,但是我本意不是,请作者谅解,只是想加深理解。十分感谢。

参考的博客 鸿洋: http://blog.csdn.net/lmj623565791/article/details/24252901


概述:实现自定义view的步骤
  1. 明确自定义view需要哪些属性,以及属性的类型取值,在style.xml中定义好
  2. 写一个自定义view的类,将自定义view的引用,写入layout文件中,以及自定义属性
  3. 自定义的view类
    (1). 重写父view类的构造方法,解析出自定义属性,并实现属性效果
    (2).重写 onMeasure(负责计算 view的大小)、onDraw(负责告诉屏幕输出什么,绘制什么)

第一步:在tyles.xml中自定义属性

tyles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>
    <!-- 此为自定义view,的自定义属性 -->
    <declare-styleable name="demo_custom_view_1">
        <attr name="textText" format="string" />
        <attr name="textSize" format="dimension" />
        <attr name="textColor" format="color" />
    </declare-styleable>
</resources>
  • name代表属性值,format代表属性类型,比如int,string等,总共有string、color、demension、integer、enum、reference、float、boolean、fraction、flag。
  • textText:将要显示的文字,为string类型
  • textSize:为文字的大小,尺寸类型
  • textColor:为文字的颜色,颜色类型
  • 在这里定义的属性可以在布局layout文件中使用,就像 “android:layout_width”这样使用一样。
  • 想在layout文件中使用,还需要在layout文件中,定义这个应用自定义view的命名空间。

第二步:在layout文件中使用自定义view以及属性
  • 需要提前创建一个自定义view类,本文是 SimulatedOfTextView
    activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
    <!--
        这里就是我写的自定义view
        如果想使用自定义属性,需要引用本应用的命名空间
    -->
    <com.qq315301615.android_study.demo_custom_view_1.custom_view.SimulatedOfTextView
        xmlns:customView="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_green_light"
        customView:textText="我是自定义文字"
        customView:textSize="20sp"
        customView:textColor="@android:color/holo_orange_dark"/>
</RelativeLayout>
  • 这里一定要注意的是 命名空间 xmlns:customView=”http://schemas.android.com/apk/res-auto”,customView可以随便写,但是后面的http://schemas.android.com/apk/res-auto是固定的,这么写就行。
  • 这里也分别对三个自定义属性,分别赋了值。当然在自定义view代码中,也需要考虑没写的默认情况。

第三步:完善自定义view类
  1. 集成 android.view.View 类,并实现其中3个构造方法,并且修改构造方法的实现
/**
 * 仿TextView,自定义类
 * 类中的构造方法的调用,为标准方法,可以直接参考系统的TextView源码
 */
public class SimulatedOfTextView extends View {
    public SimulatedOfTextView(Context context) {
        this(context, null);
    }
    public SimulatedOfTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public SimulatedOfTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initConstructor(context, attrs, defStyleAttr);
    }
    /**
     * 以上三个构造方法将依次地调用,最后将会调用这个方法
     *
     * @param context 应用的上下文
     * @param attrs 在layout文件定义的属性的集合
     * @param defStyleAttr 默认的属性值,一般为0
     */
    public void initConstructor(Context context, AttributeSet attrs, int defStyleAttr) {
    }
}
  1. 接下来,解析layout中定义的属性
  2. 3.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值