Android -- TypedArray

摘要:
xmlversion=“1.0”encoding=“utf-8”;声明styleablename=“MyView”>其中resource是标记;layout_ height=“wrap_content”android;myColor=“#3324243”/>&lt:myapp=“http;AttributeSetattrs){super(context;

当我们自定义View的时候,在给View赋值一些长度宽度的时候,一般都是在layout布局文件中进行的。,比如android:layout_height="wrap_content",除此之外,我们也可以自己定义属性,这样在使用的时候我们就可以使用形如 myapp:myTextSize="20sp"的方式了。

values/attrs.xml

首先要创建变量,创建了个values/attrs.xml文件,或文件名任意,但是要在values目录下:

<?xml version="1.0" encoding="utf-8"?>   
<resources>   
    <declare-styleable name="MyView">   
        <attr name="textSize" format="dimension" />   
    </declare-styleable>   
</resources>

其中resource是跟标签,可以在里面定义若干个declare-styleable,<declare-styleable name="MyView">中name定义了变量的名称,下面可以再自定义多个属性,针对<attr name="textSize" format="dimension"/>来说,其属性的名称为"textSize",format指定了该属性类型为dimension,只能表示字体的大小。

  • format还可以指定其他的类型比如:
  • reference   表示引用,参考某一资源ID
  • string   表示字符串
  • color   表示颜色值
  • dimension   表示尺寸值
  • boolean   表示布尔值
  • integer   表示整型值
  • float   表示浮点值
  • fraction   表示百分数
  • enum   表示枚举值
  • flag   表示位运算

layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:myapp="http://schemas.android.com/apk/res/com.eyu.attrtextdemo"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    tools:context=".MainActivity" >  

    <us.yydcdut.MyView  
        android:layout_height="wrap_content"  
        android:layout_width="wrap_content"  
        myapp:textSize="20sp"  
        myapp:myColor="#324243"/>  
  
</LinearLayout>

可以看到多了xmlns:myapp="http://schemas.android.com/apk/res/com.eyu.attrtextdemo"以及在自定义View中myapp:textSize="20sp" ,myapp:myColor="#324243" 

obtainStyledAttributes

context通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设置

obtainStyledAttributes方法有三个,我们最常用的是有一个参数的obtainStyledAttributes(int[] attrs),其参数直接styleable中获得

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);

调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响  

public class MyView extends View{  
    public Paint paint;  
  
    public MyView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        paint = new Paint();  
          
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);      
        int textColor = a.getColor(R.styleable.MyView_myColor, 003344);  
        float textSize = a.getDimension(R.styleable.MyView_myTextSize, 33);  
        paint.setTextSize(textSize);  
        paint.setColor(textColor);  
        a.recycle();  
    }  
  
    public MyView(Context context) {  
        super(context);  
    }  
      
    @Override 
    protected void onDraw(Canvas canvas) {  
        super.onDraw(canvas);     
        paint.setStyle(Style.FILL);  
        canvas.drawText("aaaaaaa", 10, 50, paint);  
    }  
      
}

我是天王盖地虎的分割线

免责声明:文章转载自《Android -- TypedArray》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇20169205实验一 Java开发环境的熟悉(Linux+IDEA)express-7 请求和响应对象(2)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

微信小程序实现国旗头像,国庆个性化头像。国庆头像

如需自取,完整项目源码:https://gitee.com/vxsoft/online-h 若对你有帮助,烦请star一个 请给我一面国旗@微信官方,先上生成的头像效果图  小程序的制作国庆头像的页面 利用 canvas 绘制头像: 核心代码: wx.canvasToTempFilePath({ x: 0,...

WebUploader上传大文件的三种解决方案

需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在500M内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以501M来进行限制。 第一步: 前端修改 由于项目使用的是BJUI前端框架,并没有使用框架本身的文件上传控件,而使用的基于jQuery的Uploadify文件上传组件,在项目使用的jslib项目中找到了BJUI框架集...

(二)PaddleOCR 编译 ocr_system.dll

 选中ocr_system项目 右键-->仅用于项目-->仅生成ocr_system, 生成ocr_system.dll,打开cmd cd到Release目录下,就可以看到ocr_system.dll了。 下图中的Debug在编译的时候记得换成Release,不然就会报下图错误列表中的错误。 这样ocr_system.dll 和 ocr_...

一个比较牛的加密算法

一个加密算法 据说此加密算法每次加密的结果都不同,但解密的结果相同 using System; namespace test001 {  /**    对sSource,进行加密、解密操作。    @param sSource String :操作的字符串    @param iFlag int :操作类型 1-加密  2-解密    @return St...

tk.mybatis 报错:tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'apiLogMapper' defined in file : Invocation of init method failed; nested exception is tk.my...

hibernate主键自动生成

Entity类中,主键尽量使用可以为null值的类型,比如Integer,Long,String等,不要用int,long等。因为如果主键为null,则表示该实体类还没有保存到数据库,是一个临时状态(Transient),而int,long不能设置为null,不具备该功能。 <!--以上抄书,待研究论证--> Java代码   <...