zoukankan      html  css  js  c++  java
  • VIEW当中自定义属性的使用

    主要有三种方法可以实现自定义属性。


    第一种方法,直接设置属性值,通过attrs.getAttributeResourceValue拿到这个属性值。

    (1)在xml文件中设置属性值

    [html] view plaincopy
    1. <com.example.activity.IconTextView   
    2.        android:layout_width="fill_parent"  
    3.     android:layout_height="wrap_content"  
    4.     android:text="@string/smile1"  
    5.     iconSrc="@drawable/smile"/>  

    (2)在构造函数中拿到这个值

    [java] view plaincopy
    1. public IconTextView(Context context, AttributeSet attrs) {  
    2.         super(context, attrs);  
    3.         resourceID = attrs.getAttributeResourceValue(null"iconSrc"0);  
    4.         if(resourceID > 0) {  
    5.             bitmap = BitmapFactory.decodeResource(getResources(), resourceID);  
    6.         }  
    7.     }  


    第二种方法,使用自己的命名空间

    (1)注意在xml文件中,需要声明一个命名空间,形式为http:// + 这个VIEW的包名

    [html] view plaincopy
    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     xmlns:mobile="http://com.example.activity"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <com.example.activity.IconTextView   
    8.         android:layout_width="fill_parent"  
    9.         android:layout_height="wrap_content"  
    10.         android:text="@string/smile1"  
    11.         mobile:iconSrc="@drawable/smile"/>  
    12.   
    13. </LinearLayout>  

    (2)通过attrs.getAttributeResourceValue,其中第一个参数为命名空间。

    [java] view plaincopy
    1. //命名空间  
    2.    private final String namespace = "http://com.example.activity"  
    [java] view plaincopy
    1.     public IconTextView(Context context, AttributeSet attrs) {  
    2.         super(context, attrs);  
    3.         resourceID = attrs.getAttributeResourceValue(namespace, "iconSrc"0);  
    4. //      TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);  
    5. //      resourceID = array.getResourceId(R.styleable.IconTextView_iconSrc, 0);  
    6.         if(resourceID > 0) {  
    7.             bitmap = BitmapFactory.decodeResource(getResources(), resourceID);  
    8.         }  
    9.     }  


    第三种方法,通过自定义attrs.xml来实现

    (1)自定义一个attrs.xml文件

    [html] view plaincopy
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <declare-styleable name="IconTextView">  
    4.         <attr name="iconSrc" format="reference"/>  
    5.     </declare-styleable>  
    6. </resources>  

    (2)在xml文件中使用这一属性,注意此时命名空间的书写规范。

    [html] view plaincopy
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     xmlns:mobile="http://schemas.android.com/apk/res/com.example.activity"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     android:orientation="vertical" >  
    7.   
    8.     <com.example.activity.IconTextView   
    9.         android:layout_width="fill_parent"  
    10.         android:layout_height="wrap_content"  
    11.         android:text="@string/smile1"  
    12.         mobile:iconSrc="@drawable/smile"/>  
    13.       
    14.     <com.example.activity.IconTextView   
    15.         android:layout_width="fill_parent"  
    16.         android:layout_height="wrap_content"  
    17.         android:text="@string/smile2"  
    18.         android:textSize="24dp"  
    19.         mobile:iconSrc="@drawable/smile"/>  
    20.       
    21.     <com.example.activity.IconTextView   
    22.         android:layout_width="fill_parent"  
    23.         android:layout_height="wrap_content"  
    24.         android:text="@string/smile3"  
    25.         android:textSize="36dp"  
    26.         mobile:iconSrc="@drawable/smile"/>  
    27.       
    28.   
    29. </LinearLayout>  

    (3)在代码中使用context.obtainStyledAttributes获得属性值

    [java] view plaincopy
    1. package com.example.activity;  
    2.   
    3. import android.content.Context;  
    4. import android.content.res.TypedArray;  
    5. import android.graphics.Bitmap;  
    6. import android.graphics.BitmapFactory;  
    7. import android.graphics.Canvas;  
    8. import android.graphics.Rect;  
    9. import android.util.AttributeSet;  
    10. import android.widget.TextView;  
    11.   
    12. public class IconTextView extends TextView {  
    13.     //命名空间  
    14.     private final String namespace = "http://com.example.activity";  
    15.     //资源ID  
    16.     private int resourceID = 0;  
    17.     private Bitmap bitmap;  
    18.   
    19.     public IconTextView(Context context, AttributeSet attrs) {  
    20.         super(context, attrs);  
    21.         TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);  
    22.         resourceID = array.getResourceId(R.styleable.IconTextView_iconSrc, 0);  
    23.         if(resourceID > 0) {  
    24.             bitmap = BitmapFactory.decodeResource(getResources(), resourceID);  
    25.         }  
    26.     }  
    27.       
    28.     @Override  
    29.     public void onDraw(Canvas canvas) {  
    30.         if (bitmap != null) {  
    31.             Rect src = new Rect(00, bitmap.getWidth(), bitmap.getHeight());  
    32.               
    33.             Rect target = new Rect();  
    34.             int textHeight = (int)getTextSize();  
    35.             target.left = 0;  
    36.             target.top =(int)(getMeasuredHeight() - getTextSize()) / 2 + 1;  
    37.             target.bottom = target.top + textHeight;  
    38.             target.right = (int)(textHeight * (bitmap.getWidth() / (float)bitmap.getHeight()));  
    39.               
    40.             canvas.drawBitmap(bitmap, src, target, getPaint());  
    41.               
    42.             canvas.translate(target.right + 20);  
    43.         }  
    44.           
    45.         super.onDraw(canvas);  
    46.     }  
    47.       
    48. }  

    第三种方法实例实现的是一个自定义的带图片的TextView,效果图如下



  • 相关阅读:
    IDEA:springboot框架使用mybatis-generator插件报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.liwenwen.redcup03.mapper.UserMapper.selectByPrimaryKey
    IDEA:springboot项目启动程序添加@MapperScan(value = "com.xxx.xxx.dao")报错:Invalid default: public abstract java.lang.Class org.mybatis.spring.annotation.MapperScan.factoryBean()
    IDEA报错:Could not find resource com/liwen/bean/userMapper.xml
    报错:Error updating database. Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'gender' at row 1
    报错:Error instantiating class com.liwen.mybatis.bean.Employee with invalid types () or values ().
    学习mybatis框架时配置xml文件解决select莫名其妙报错问题
    控制台报错Cause: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 78; 元素类型 "select" 必须后跟属性规范 ">" 或 "/>"
    oracle查看行锁表锁
    07版word解析
    HWPFDocument读取doc,wps文档(含图片读取)
  • 原文地址:https://www.cnblogs.com/zsw-1993/p/4879470.html
Copyright © 2011-2022 走看看