[Android]attrs.xml的自定义VIEw

来源:互联网 发布:sap软件费用 编辑:程序博客网 时间:2024/05/17 08:27
[Android]attrs.xml文件中属性类型format值的格式


"reference" //引用  
"color" //颜色  
"boolean" //布尔值  
"dimension" //尺寸值  
"float" //浮点值  
"integer" //整型值  
"string" //字符串  
"fraction" //百分数,比如200%  


Android中在values中定义一个attrs.xml,然后自己定义一个组件MyView

attrs.xml内容如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>       
  2. <resources>       
  3.     <declare-styleable name="MyView">       
  4.         <attr name="textColor" format="color" />       
  5.         <attr name="textSize" format="dimension" />       
  6.     </declare-styleable>       
  7. </resources>   

定义的组件MyView:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.jiangwei.demo;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.TypedArray;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Paint.Style;  
  9. import android.graphics.Rect;  
  10. import android.util.AttributeSet;  
  11. import android.view.View;  
  12.   
  13. public class MyView extends View {  
  14. private Paint mPaint;  
  15. private static final String mString = "Welcome to Mr Wei's blog";  
  16. public MyView(Context context) {  
  17. super(context);  
  18. mPaint = new Paint();  
  19. }  
  20.   
  21. public MyView(Context context, AttributeSet attrs) {  
  22. super(context, attrs);  
  23. mPaint = new Paint();  
  24. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView);  
  25. int textColor = a.getColor(R.styleable.MyView_textColor, 0XFFFFFFFF);  
  26. float textSize = a.getDimension(R.styleable.MyView_textSize, 36);  
  27. mPaint.setTextSize(textSize);  
  28. mPaint.setColor(textColor);  
  29. a.recycle();  
  30. }  
  31.   
  32. @Override  
  33. protected void onDraw(Canvas canvas) {  
  34. // TODO Auto-generated method stub  
  35. super.onDraw(canvas);  
  36. // 设置填充  
  37. mPaint.setStyle(Style.FILL);  
  38. // 画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标  
  39. canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);  
  40. mPaint.setColor(Color.BLUE);  
  41. // 绘制文字  
  42. canvas.drawText(mString, 10, 110, mPaint);  
  43. }  
  44. }  

main.xml内容:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:test="http://schemas.android.com/apk/res/com.jiangwei.demo"//一定记得添加前缀  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     android:orientation="vertical" >  
  8.     <com.jiangwei.demo.MyView  
  9. android:layout_width="fill_parent"   
  10.    android:layout_height="fill_parent"   
  11.    test:textSize="20px"//test是个前缀  
  12.    test:textColor="#ffffff"/>  
  13. </LinearLayout>  


具体内容:

格式如上,其中“xmlns:wen”冒号后面是标签名,在下面使用时(只对当前文件可用)
<TextView  wen:属性名/>
“com.iteye.googlers”是你的工程包名。
1. reference:参考某一资源ID。
    (1)属性定义:
            

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <declare-styleable name = "名称">  
  2.                    <attr name = "background" format = "reference" />  
  3. </declare-styleable>  

    (2)属性使用:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <ImageView  
  2.                      android:layout_width = "42dip"  
  3.                      android:layout_height = "42dip"  
  4.                      android:background = "@drawable/图片ID"  
  5.                      />  


2. color:颜色值。
    (1)属性定义:
            

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <declare-styleable name = "名称">  
  2.                    <attr name = "textColor" format = "color" />  
  3.             </declare-styleable>  

    (2)属性使用:
           
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <TextView  
  2.                     android:layout_width = "42dip"  
  3.                     android:layout_height = "42dip"  
  4.                     android:textColor = "#00FF00"  
  5.                     />  

 3. boolean:布尔值。
    (1)属性定义:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <declare-styleable name = "名称">  
  2.                 <attr name = "focusable" format = "boolean" />  
  3. </declare-styleable>  

    (2)属性使用:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <Button  
  2.                    android:layout_width = "42dip"  
  3.                    android:layout_height = "42dip"  
  4.                    android:focusable = "true"/>  

4. dimension:尺寸值。
     (1)属性定义:
             
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <declare-styleable name = "名称">  
  2.                    <attr name = "layout_width" format = "dimension" />  
  3. </declare-styleable>  

    (2)属性使用:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <Button  
  2.                    android:layout_width = "42dip"  
  3.                    android:layout_height = "42dip"  
  4.                   />  

 5. float:浮点值。
    (1)属性定义:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <declare-styleable name = "AlphaAnimation">  
  2.                    <attr name = "fromAlpha" format = "float" />  
  3.                    <attr name = "toAlpha" format = "float" />  
  4. </declare-styleable>  

    (2)属性使用:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <alpha  
  2.        android:fromAlpha = "1.0"  
  3.        android:toAlpha = "0.7"  
  4. />  
 
6. integer:整型值。
    (1)属性定义:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <declare-styleable name = "AnimatedRotateDrawable">  
  2.                    <attr name = "visible" />  
  3.                    <attr name = "frameDuration" format="integer" />  
  4.                    <attr name = "framesCount" format="integer" />  
  5.                    <attr name = "pivotX" />  
  6.                    <attr name = "pivotY" />  
  7.                    <attr name = "drawable" />  
  8. </declare-styleable>  

    (2)属性使用:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <animated-rotate  
  2.                    xmlns:android = "http://schemas.android.com/apk/res/android"    
  3.                    android:drawable = "@drawable/图片ID"    
  4.                    android:pivotX = "50%"    
  5.                    android:pivotY = "50%"    
  6.                    android:framesCount = "12"    
  7.                    android:frameDuration = "100"  
  8.                    />  
 
7. string:字符串。
    (1)属性定义:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <declare-styleable name = "MapView">  
  2.                    <attr name = "apiKey" format = "string" />  
  3. </declare-styleable>  

    (2)属性使用:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <com.google.android.maps.MapView  
  2.                     android:layout_width = "fill_parent"  
  3.                     android:layout_height = "fill_parent"  
  4.                     android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"  
  5.                     />  

8. fraction:百分数。
     (1)属性定义:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <declare-styleable name="RotateDrawable">  
  2.                    <attr name = "visible" />  
  3.                    <attr name = "fromDegrees" format = "float" />  
  4.                    <attr name = "toDegrees" format = "float" />  
  5.                    <attr name = "pivotX" format = "fraction" />  
  6.                    <attr name = "pivotY" format = "fraction" />  
  7.                    <attr name = "drawable" />  
  8. </declare-styleable>  

    (2)属性使用:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <rotate  
  2.                  xmlns:android = "http://schemas.android.com/apk/res/android"   
  3.                android:interpolator = "@anim/动画ID"  
  4.                  android:fromDegrees = "0"   
  5.                android:toDegrees = "360"  
  6.                  android:pivotX = "200%"  
  7.                  android:pivotY = "300%"   
  8.                android:duration = "5000"  
  9.                  android:repeatMode = "restart"  
  10.                  android:repeatCount = "infinite"  
  11.                 />  

9. enum:枚举值。
    (1)属性定义:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <declare-styleable name="名称">  
  2.                    <attr name="orientation">  
  3.                           <enum name="horizontal" value="0" />  
  4.                           <enum name="vertical" value="1" />  
  5.                    </attr>              
  6. </declare-styleable>  

    (2)属性使用:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <LinearLayout  
  2.                     xmlns:android = "http://schemas.android.com/apk/res/android"  
  3.                     android:orientation = "vertical"  
  4.                     android:layout_width = "fill_parent"  
  5.                     android:layout_height = "fill_parent"  
  6.                     >  
  7. </LinearLayout>  

10. flag:位或运算。
     (1)属性定义:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <declare-styleable name="名称">  
  2.                    <attr name="windowSoftInputMode">  
  3.                            <flag name = "stateUnspecified" value = "0" />  
  4.                            <flag name = "stateUnchanged" value = "1" />  
  5.                            <flag name = "stateHidden" value = "2" />  
  6.                            <flag name = "stateAlwaysHidden" value = "3" />  
  7.                            <flag name = "stateVisible" value = "4" />  
  8.                            <flag name = "stateAlwaysVisible" value = "5" />  
  9.                            <flag name = "adjustUnspecified" value = "0x00" />  
  10.                            <flag name = "adjustResize" value = "0x10" />  
  11.                            <flag name = "adjustPan" value = "0x20" />  
  12.                            <flag name = "adjustNothing" value = "0x30" />  
  13.                     </attr>           
  14. lt;/declare-styleable>  

     (2)属性使用:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <activity  
  2.       android:name = ".StyleAndThemeActivity"  
  3.       android:label = "@string/app_name"  
  4.       android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">  
  5.       <intent-filter>  
  6.             <action android:name = "android.intent.action.MAIN" />  
  7.             <category android:name = "android.intent.category.LAUNCHER" />  
  8.       </intent-filter>  
  9. </activity>  


     注意:
     属性定义时可以指定多种类型值。
    (1)属性定义:
            
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <declare-styleable name = "名称">  
  2.       <attr name = "background" format = "reference|color" />  
  3. </declare-styleable>  

    (2)属性使用:
             
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <ImageView  
  2.         android:layout_width = "42dip"  
  3.         android:layout_height = "42dip"  
  4.         android:background = "@drawable/图片ID|#00FF00"  
  5.         />  
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孕妇误吃桃胶了怎么办? 孕4个月吃了桃胶怎么办 刚怀孕吃了桃胶怎么办 额头被打了个包怎么办 裤子被84掉颜色怎么办 高中生晚上偷家里电脑上网怎么办 住高层睡不好觉怎么办 水瓶座如果恨我们了该怎么办 不锈钢保温瓶不保温了怎么办 壁纸颜色选深了怎么办 客厅壁纸太暗了怎么办 别人说你衣服丑怎么办 高楼热水器风大熄火怎么办 1楼独立下水2楼怎么办 宜家家具不会装怎么办 服务行业遇到低素质客户怎么办 服务类没有进项票怎么办 教师对学生缺乏耐心怎么办 买了竹料烂尾楼怎么办 刚毕业想换工作怎么办 客厅灯变不了光怎么办 塑料镀铝浸底漆咬底怎么办 标志408钥匙掉了怎么办 房本测绘页丢了怎么办 房本测绘页信息有误怎么办 税务登记证办完没有年检怎么办 建筑施工升降机司机证怎么办 北京建筑施工证怎么办呢 模拟城市5水抽干了怎么办 ip地址错误网络无法接通怎么办 rhino模型太大打开半天怎么办 日本新干线车票丢了怎么办 房间太干燥怎么办又热 薄荷叶子全干了怎么办 水培栀子花叶子蔫了怎么办 薄荷叶叶边干了怎么办 碗莲叶子发黑腐烂怎么办 龟背叶叶子蔫了怎么办 夏天龟背竹蔫了怎么办 春羽叶子长黄斑怎么办 百合竹叶子发黄掉落怎么办