地址:http://blog.csdn.net/lovecluo/article/details/8699121
有时候在做开发的时候,android提供给我们的视图并不能满足我们的要求,所以有时候我们需要自己创建自己的view。
我们只需要将我们想要的继承于View,然后重写里面的方法就可以了。
- package com.example.view;
- import android.content.Context;
- import android.graphics.Color;
- import android.util.AttributeSet;
- import android.widget.TextView;
- public class MyTextView extends TextView {
- public MyTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- this.setTextColor(Color.BLUE);// 将字体设置成蓝色
- }
- }
然后我们只需要在Layout中使用这个view就可以了。
- <?xml version="1.0" encoding="UTF-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <com.example.myviewtest01
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:text="@string/hello_world" />
- </RelativeLayout>
如果我们要自定义属性,就像android:layout_height="wrap_content"这种:
首先我们要学习declare-styleable,它是给自定义控件添加自定义属性时用的。
我们在res/values下建立一个myAttrs.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="MyTextView">
- <attr name="fontSize" format="dimension" />
- </declare-styleable>
- </resources>
解释一下上面那些值的属性
Name=”MyTextView” 是在R.java文件生成对应的引用名。
<attr name="fontSize"format="dimension" />这个就是自定义的属性名称。在R.java中会生成对应的名字,这个地方是MyTextView_fontSize.
后面的format是定义的你的变量的属性。如果你想学习有关他的更多详细信息,请转向:http://blog.csdn.net/lihengfang/article/details/8290754
在布局文件中我们就可以直接这样使用:
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:mytextview="http://schemas.android.com/apk/res/com.example.myviewtest"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <com.example.myviewtest.MyTextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- mytextview:fontSize="20dp"
- android:text="text size = 20dp" >
- </com.example.myviewtest.MyTextView>
- <com.example.myviewtest.MyTextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- mytextview:fontSize="15dp"
- android:text="text size=15dp" >
- </com.example.myviewtest.MyTextView>
- </LinearLayout>
其中xmlns:mytextview=http://schemas.android.com/apk/res/com.examp"中的地址是指向R.java所在的目录,我开始也不知道这个是干什么的,但是实验了一次,我就知道了。我开始以为那个是指向我们自定义的那个类,后面才发现,我错了。
我们自定义的View中,我们要获取给定的属性值,如下代码:
- package com.example.myviewtest;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Canvas;
- import android.util.AttributeSet;
- import android.widget.TextView;
- public class MyTextView extends TextView {
- public MyTextView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- // TODO Auto-generated constructor stub
- }
- public MyTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MyTextView);
- String name = ta.getString(R.styleable.MyTextView_fontSize);
- System.out.println("name=" + name);
- this.setTextSize(ta.getDimension(R.styleable.MyTextView_fontSize, 10));
- }
- public MyTextView(Context context) {
- super(context);
- }
- }