Android中自定义组件及自定义属性
2015-02-12 14:51
381 查看
地址:http://blog.csdn.net/lovecluo/article/details/8699121
有时候在做开发的时候,android提供给我们的视图并不能满足我们的要求,所以有时候我们需要自己创建自己的view。
我们只需要将我们想要的继承于View,然后重写里面的方法就可以了。
[java]
view plaincopy
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就可以了。
[html]
view plaincopy
<?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
[html]
view plaincopy
<?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是定义的你的变量的属性。如果你想学习有关他的更多详细信息,请转向:/article/8736874.html
在布局文件中我们就可以直接这样使用:
[html]
view plaincopy
<?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中,我们要获取给定的属性值,如下代码:
[java]
view plaincopy
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);
}
}
有时候在做开发的时候,android提供给我们的视图并不能满足我们的要求,所以有时候我们需要自己创建自己的view。
我们只需要将我们想要的继承于View,然后重写里面的方法就可以了。
[java]
view plaincopy
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就可以了。
[html]
view plaincopy
<?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
[html]
view plaincopy
<?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是定义的你的变量的属性。如果你想学习有关他的更多详细信息,请转向:/article/8736874.html
在布局文件中我们就可以直接这样使用:
[html]
view plaincopy
<?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中,我们要获取给定的属性值,如下代码:
[java]
view plaincopy
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);
}
}
相关文章推荐
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- Android定制组件之图文 (自定义组件图) + TableLayout属性
- 对于android中自定义组件属性的理解
- android中xml命名空间、自定义组件属性
- android图像图像处理——自定义组件的属性
- Android 两种为自定义组件添加属性的使用方法和区别
- android attrs组件自定义属性
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- Android自定义组件的属性
- Android,xml组件属性style的使用,自定义适配器(含持有者模式)
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- Android自定义组件添加组件的属性
- android 自定义属性通过反射关联组件与方法
- Android自定义组件简单实例(自定义属性)
- 说说Android 两种为自定义组件添加属性的使用方法和区别 推荐
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- Android 两种为自定义组件添加属性的使用方法和区别
- Android 使用自定义组件和自定义属性
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- 说说Android 两种为自定义组件添加属性的使用方法和区别