Android 之 自定义View

本文介绍如何在Android中创建自定义View,包括定义属性、获取属性值及使用这些属性进行绘图的方法。通过实例演示如何设置文本、颜色和大小等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

弘扬博客

自定义View的步骤:

1、自定义View的属性

2、在View的构造方法中获得我们自定义的属性

[ 3、重写onMesure ]

4、重写onDraw

1、自定义View的属性,首先在res/values/  下建立一个attrs.xml , 在里面定义我们的属性和声明我们的整个样式。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <attr name="titleText" format="string" />  
  5.     <attr name="titleTextColor" format="color" />  
  6.     <attr name="titleTextSize" format="dimension" />  
  7.   
  8.     <declare-styleable name="CustomTitleView">  
  9.         <attr name="titleText" />  
  10.         <attr name="titleTextColor" />  
  11.         <attr name="titleTextSize" />  
  12.     </declare-styleable>  
  13.   
  14. </resources>  
我们定义了字体,字体颜色,字体大小3个属性,format是值该属性的取值类型:

一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;不清楚的可以google一把。

然后在布局中声明我们的自定义View

然后在布局中声明我们的自定义View

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     xmlns:custom="http://schemas.android.com/apk/res/com.example.customview01"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" >  
  6.   
  7.     <com.example.customview01.view.CustomTitleView  
  8.         android:layout_width="200dp"  
  9.         android:layout_height="100dp"  
  10.         custom:titleText="3712"  
  11.         custom:titleTextColor="#ff0000"  
  12.         custom:titleTextSize="40sp" />  
  13.   
  14. </RelativeLayout>  

一定要引入 xmlns:custom="http://schemas.android.com/apk/res/com.example.customview01"我们的命名空间,后面的包路径指的是项目的package

2、在View的构造方法中,获得我们的自定义的样式

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * 文本 
  3.      */  
  4.     private String mTitleText;  
  5.     /** 
  6.      * 文本的颜色 
  7.      */  
  8.     private int mTitleTextColor;  
  9.     /** 
  10.      * 文本的大小 
  11.      */  
  12.     private int mTitleTextSize;  
  13.   
  14.     /** 
  15.      * 绘制时控制文本绘制的范围 
  16.      */  
  17.     private Rect mBound;  
  18.     private Paint mPaint;  
  19.   
  20.     public CustomTitleView(Context context, AttributeSet attrs)  
  21.     {  
  22.         this(context, attrs, 0);  
  23.     }  
  24.   
  25.     public CustomTitleView(Context context)  
  26.     {  
  27.         this(context, null);  
  28.     }  
  29.   
  30.     /** 
  31.      * 获得我自定义的样式属性 
  32.      *  
  33.      * @param context 
  34.      * @param attrs 
  35.      * @param defStyle 
  36.      */  
  37.     public CustomTitleView(Context context, AttributeSet attrs, int defStyle)  
  38.     {  
  39.         super(context, attrs, defStyle);  
  40.         /** 
  41.          * 获得我们所定义的自定义样式属性 
  42.          */  
  43.         TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyle, 0);  
  44.         int n = a.getIndexCount();  
  45.         for (int i = 0; i < n; i++)  
  46.         {  
  47.             int attr = a.getIndex(i);  
  48.             switch (attr)  
  49.             {  
  50.             case R.styleable.CustomTitleView_titleText:  
  51.                 mTitleText = a.getString(attr);  
  52.                 break;  
  53.             case R.styleable.CustomTitleView_titleTextColor:  
  54.                 // 默认颜色设置为黑色  
  55.                 mTitleTextColor = a.getColor(attr, Color.BLACK);  
  56.                 break;  
  57.             case R.styleable.CustomTitleView_titleTextSize:  
  58.                 // 默认设置为16sp,TypeValue也可以把sp转化为px  
  59.                 mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(  
  60.                         TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));  
  61.                 break;  
  62.   
  63.             }  
  64.   
  65.         }  
  66.         a.recycle();  
  67.   
  68.         /** 
  69.          * 获得绘制文本的宽和高 
  70.          */  
  71.         mPaint = new Paint();  
  72.         mPaint.setTextSize(mTitleTextSize);  
  73.         // mPaint.setColor(mTitleTextColor);  
  74.         mBound = new Rect();  
  75.         mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);  
  76.   
  77.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值