慢慢成长路——自定义view(2),关于自定义属性的深入解析

再查看源码的属性设置时候,注意到了spanable这个属性,于是去查了查!发现真的特别强大:

 1、BackgroundColorSpan 背景色 
2、ClickableSpan 文本可点击,有点击事件
3、ForegroundColorSpan 文本颜色(前景色)
4、MaskFilterSpan 修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
5、MetricAffectingSpan 父类,一般不用
6、RasterizerSpan 光栅效果
7、StrikethroughSpan 删除线(中划线)
8、SuggestionSpan 相当于占位符
9、UnderlineSpan 下划线
10、AbsoluteSizeSpan 绝对大小(文本字体)
11、DynamicDrawableSpan 设置图片,基于文本基线或底部对齐。
12、ImageSpan 图片
13、RelativeSizeSpan 相对大小(文本字体)
14、ReplacementSpan 父类,一般不用
15、ScaleXSpan 基于x轴缩放
16、StyleSpan 字体样式:粗体、斜体等
17、SubscriptSpan 下标(数学公式会用到)
18、SuperscriptSpan 上标(数学公式会用到)
19、TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色)
20、TypefaceSpan 文本字体
21、URLSpan 文本超链接

以上是这个属性可以用的所有方法,比如文字点击事件,我以前都是设置两个TextView设置点击事件好不好,现在可以直接用ClickableSpan就可以直接实现点击文字了。

 

--------------------------------------------------------------------------分----------------割---------------线-------------------------------------------------------------------

 

 

好了,咱们进入正题说一说自定义属性吧,首先,我们使用快捷键ctrl+shift+n(在下使用的编译工具是AS),然后找到attrs.xml文件,打开里面就是所有的属性。具体来分析一下

 

<declare-styleable name="TextView">
    <attr name="bufferType">
        <enum name="normal" value="0" />
        <enum name="spannable" value="1" />     
        <enum name="editable" value="2" />
    </attr>
    <attr name="text" format="string" localization="suggested" />
    <attr name="hint" format="string" />
    <attr name="textColor" />
    <attr name="textColorHighlight" />
    <attr name="textColorHint" />
    <attr name="textAppearance" />
    <attr name="textSize" />
    <attr name="textScaleX" format="float" />
    <attr name="typeface" /> 
    <attr name="textStyle" /> 
    <attr name="fontFamily" />
  
    <attr name="textColorLink" />
    <!-- Makes the cursor visible (the default) or invisible. -->
    <attr name="cursorVisible" format="boolean" />
    <attr name="maxLines" format="integer" min="0" />
    <attr name="maxHeight" />
    <attr name="lines" format="integer" min="0" />
    <attr name="height" format="dimension" />
    <attr name="minLines" format="integer" min="0" />
    <attr name="minHeight" />
    <attr name="maxEms" format="integer" min="0" />
    <attr name="maxWidth" />
    <attr name="ems" format="integer" min="0" />
    <attr name="width" format="dimension" />
    <attr name="minEms" format="integer" min="0" />
    <attr name="minWidth" />
    <attr name="gravity" />
    <attr name="scrollHorizontally" format="boolean" />
    <attr name="password" format="boolean" />
 
    <attr name="singleLine" format="boolean" />
 
    <attr name="enabled" format="boolean" />
    <!-- If the text is selectable, select it all when the view takes
         focus. -->
    <attr name="selectAllOnFocus" format="boolean" />
    <!-- Leave enough room for ascenders and descenders instead of
         using the font ascent and descent strictly.  (Normally true). -->
    <attr name="includeFontPadding" format="boolean" />
    <!-- Set an input filter to constrain the text length to the
         specified number. -->
    <attr name="maxLength" format="integer" min="0" />
   
    <attr name="shadowColor" />
    <!-- Horizontal offset of the text shadow. -->
    <attr name="shadowDx" />
    <!-- Vertical offset of the text shadow. -->
    <attr name="shadowDy" />
    <!-- Blur radius of the text shadow. -->
    <attr name="shadowRadius" />
    <attr name="autoLink" />
   
    <attr name="linksClickable" format="boolean" />
  
    <attr name="numeric">
        <!-- Input is numeric. -->
        <flag name="integer" value="0x01" />
        <!-- Input is numeric, with sign allowed. -->
        <flag name="signed" value="0x03" />
        <!-- Input is numeric, with decimals allowed. -->
        <flag name="decimal" value="0x05" />
    </attr>
   
  
   
</declare-styleable>

<declare-styleable>是你的view类名<attr>中是类中的变量名,<attr>中包含了许多属性,你可以将变量的值设置成固定的,也可以申明它的类型

 

在这里添加了你类中的属性之后,就可以xml文件中调用你自定义的控件时使用这些属性了,但是别忘了要在前面加上命名空间,距离如下:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:openxu="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.example.administrator.myapplication.MagicTextView
        android:id="@+id/id_text"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        openxu:text="111111111111111111111111111111111111"
        openxu:textColor="#000"
        openxu:textSize="20dp" />

</LinearLayout>

 

 

 

xmlns:openxu=“····························此处省略”这条命名空间必须加在父布局中,这里讲一下获取命名空间的办法:

 如果我们自定义属性,这个属性应该去我们的应用程序包中找,所以要引入我们应用包的命名空间xmlns:openxu="http://schemas.android.com/apk/res-auto”,res-auto表示自动查找,还有一种写法xmlns:openxu="http://schemas.android.com/apk/com.example.openxu.myview"com.example.openxu.myview为我们的应用程序包名。

 

 

当自定义完毕这些属性之后,我们发现,我们并没有定义的属性,依然可以使用,比如我上方并没有定义id,layout_width这些属性,这是因为TextView继承自View,View所拥有的所有属性它都可以使用(其他控件同理)

好啦,现在重点说一下,引用前面所说的学习文章的一句话:

 翻阅系统的属性文件,你会发现,有的这种形式,有的是;这两种的区别就是attr标签后面带不带format属性,如果带format的就是在定义属性,如果不带format的就是在使用已有的属性,name的值就是属性的名字,format是限定当前定义的属性能接受什么值。

当我们自定义自己的属性的时候,可以使用父类的属性,并且不用去fmart声明变量类型,这个在上面已经说到了,下面要说的是在使用系统自带已有的变量时,要加上其命名空间,比如说我们继承的是View,我们需要用TextView中的text属性,attrs中的name就不可以直接写成text,而是应该是:android:text;

 

<declare-styleable name="MyTextView">

<!--声明MyTextView需要使用系统定义过的text属性,注意前面需要加上android命名-->

<attr name="android:text" />

<attr name="mTextColor" format="color" />

<attr name="mTextSize" format="dimension" />

</declare-styleable>

 

在构造函数中获取属性值:

 

在上一篇文章中已经提到过,使用TypedArray来获取

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值