Android中自定义组件及自定义属性

地址:http://blog.csdn.net/lovecluo/article/details/8699121

有时候在做开发的时候,android提供给我们的视图并不能满足我们的要求,所以有时候我们需要自己创建自己的view

我们只需要将我们想要的继承于View,然后重写里面的方法就可以了。

  1. package com.example.view;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Color;  
  5. import android.util.AttributeSet;  
  6. import android.widget.TextView;  
  7.   
  8. public class MyTextView extends TextView {  
  9.   
  10.     public MyTextView(Context context, AttributeSet attrs) {  
  11.         super(context, attrs);  
  12.         this.setTextColor(Color.BLUE);// 将字体设置成蓝色  
  13.     }  
  14.   
  15. }  


然后我们只需要在Layout中使用这个view就可以了。

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" >  
  6.   
  7.     <com.example.myviewtest01  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:layout_centerVertical="true"  
  12.         android:text="@string/hello_world" />  
  13.   
  14. </RelativeLayout>  

 

如果我们要自定义属性,就像android:layout_height="wrap_content"这种:

首先我们要学习declare-styleable,它是给自定义控件添加自定义属性时用的。

我们在res/values下建立一个myAttrs.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <declare-styleable name="MyTextView">  
  5.         <attr name="fontSize" format="dimension" />  
  6.     </declare-styleable>  
  7.   
  8. </resources>  


 

解释一下上面那些值的属性

Name=”MyTextView” 是在R.java文件生成对应的引用名。

<attr name="fontSize"format="dimension" />这个就是自定义的属性名称。在R.java中会生成对应的名字,这个地方是MyTextView_fontSize.

后面的format是定义的你的变量的属性。如果你想学习有关他的更多详细信息,请转向:http://blog.csdn.net/lihengfang/article/details/8290754

 

在布局文件中我们就可以直接这样使用:

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     xmlns:mytextview="http://schemas.android.com/apk/res/com.example.myviewtest"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <com.example.myviewtest.MyTextView  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         mytextview:fontSize="20dp"  
  13.         android:text="text size = 20dp" >  
  14.     </com.example.myviewtest.MyTextView>  
  15.     <com.example.myviewtest.MyTextView  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         mytextview:fontSize="15dp"  
  19.         android:text="text size=15dp" >  
  20.     </com.example.myviewtest.MyTextView>  
  21.   
  22. </LinearLayout>  


其中xmlns:mytextview=http://schemas.android.com/apk/res/com.examp"中的地址是指向R.java所在的目录,我开始也不知道这个是干什么的,但是实验了一次,我就知道了。我开始以为那个是指向我们自定义的那个类,后面才发现,我错了。

我们自定义的View中,我们要获取给定的属性值,如下代码:

  1. package com.example.myviewtest;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.TypedArray;  
  5. import android.graphics.Canvas;  
  6. import android.util.AttributeSet;  
  7. import android.widget.TextView;  
  8.   
  9. public class MyTextView extends TextView {  
  10.   
  11.     public MyTextView(Context context, AttributeSet attrs, int defStyle) {  
  12.         super(context, attrs, defStyle);  
  13.         // TODO Auto-generated constructor stub  
  14.     }  
  15.   
  16.     public MyTextView(Context context, AttributeSet attrs) {  
  17.         super(context, attrs);  
  18.         TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MyTextView);  
  19.         String name = ta.getString(R.styleable.MyTextView_fontSize);  
  20.         System.out.println("name=" + name);  
  21.         this.setTextSize(ta.getDimension(R.styleable.MyTextView_fontSize, 10));  
  22.     }  
  23.   
  24.     public MyTextView(Context context) {  
  25.         super(context);  
  26.           
  27.     }  
  28.   
  29.       
  30.   
  31. }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值