android 自定义set,Android使用AttributeSet自定义控件的方法

本文详细介绍了如何在Android中自定义View时,通过attrs.xml和AttributeSet获取并设置自定义控件的属性,如文本、颜色和背景,以及如何在布局文件中使用自定义命名空间。通过实例展示了如何实现类似android:text的直接设置方式。
摘要由CSDN通过智能技术生成

当系统提供的控件,无法满足我们的需求的时候,我们往往会 想到自定义控件,通过继承View来实现。当我们想像 系统提供的控件那样可以如android:text="hello",这样设置自定义控件中的文字显示去,这时我们该怎么办?

带着问题,我们会想到在继承View的构造函数中 有个AttributeSet这个属性,没错  这就是 突破口。

我们可以在attrs.xml中声明自己控件的属性,在布局xml文档中声明自己的命名空间,这时就可以对设置自己想要的值了,然后在AttributeSet这个属性中获取对应的值。好了不多说,我们来看下代码,一切尽在不言中:

工程下载地址:点击打开链接

第一步:

在/res/values下编写一个attrs.xml的文件,其中内容结构如下:

其中要说的是format 格式,对于它的格式说明将在文章后面介绍。

第二步:

写一个自定义的Veiw,如下代码:

public class myView extends View{

private Paint paint;

private int bg_color,text_color,alpha;

private String text;

public myView(Context context, AttributeSet attrs) {

super(context, attrs);

paint = new Paint();

TypedArray s = context.obtainStyledAttributes(attrs, R.styleable.my_view);//从xml那传来的一组值

bg_color = s.getColor(R.styleable.my_view_bg_color, R.color.transparent);//得到attrs中对应属性的bg_color值

text_color = s.getColor(R.styleable.my_view_text_color, android.R.color.black);//..

text = s.getText(R.styleable.my_view_text).toString();//..

}

@Override

public void draw(Canvas canvas) {

// TODO Auto-generated method stub

super.draw(canvas);

paint.setColor(bg_color);

Rect r = new Rect(1, 80, 60, 140);

canvas.drawRect(r, paint);

paint.setColor(text_color);

canvas.drawText(text, 10, 100, paint);

}

}

注释在重点地方已经写清楚了,也就值AttributeSet attrs 的使用,通过它可以获取在XML中定义的值。

第三步:

在布局文档中声明你的View

xmlns:app="http://schemas.android.com/apk/res/com.example.defineview"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

app:text="nihao"

app:text_color="#ff0000"

app:bg_color="#00ff00"

/>

其中

xmlns:app="http://schemas.android.com/apk/res/com.example.defineview"

是自己声明的命名空间,app是头自由命名,在res/+自己的包名

这时你就可以使用

app:text="nihao"

这样的属性了,注意text、bg_color这些属性是在attrs中设置好的。

好了这样就OK了我们来看下运行效果:

运行效果中是不是显示出了

app:text="nihao"

app:text_color="#ff0000"

app:bg_color="#00ff00"

的效果。

我们看下文件的目录结构

附录:attrs

在这里,需要补充attrs属性的相关知识,即Attr属性是如何在XML中定义的,自定义属性的Value值可以有10种类型以及其类型的组合值,其具体使用方法如下:

1. reference:参考某一资源ID。

(1)属性定义:

(2)属性使用:

android:layout_width = "42dip"

android:layout_height = "42dip"

android:background = "@drawable/图片ID" />

2. color:颜色值。

(1)属性定义:

(2)属性使用:

android:layout_width = "42dip"

android:layout_height = "42dip"

android:textColor = "#00FF00"/>

3. boolean:布尔值。

(1)属性定义:

(2)属性使用:

android:layout_width = "42dip"

android:layout_height = "42dip"

android:focusable = "true"/>

4. dimension:尺寸值。

(1)属性定义:

(2)属性使用:

android:layout_width = "42dip"

android:layout_height = "42dip"/>

5. float:浮点值。

(1)属性定义:

(2)属性使用:

android:fromAlpha = "1.0"

android:toAlpha = "0.7"/>

6. integer:整型值。

(1)属性定义:

(2)属性使用:

xmlns:android = "http://schemas.android.com/apk/res/android"

android:drawable = "@drawable/图片ID"

android:pivotX = "50%"

android:pivotY = "50%"

android:framesCount = "12"

android:frameDuration = "100" />

7. string:字符串。

(1)属性定义:

(2)属性使用:

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"

/>

8. fraction:百分数。

(1)属性定义:

(2)属性使用:

xmlns:android = "http://schemas.android.com/apk/res/android"

android:interpolator = "@anim/动画ID"

android:fromDegrees = "0"

android:toDegrees = "360"

android:pivotX = "200%"

android:pivotY = "300%"

android:duration = "5000"

android:repeatMode = "restart"

android:repeatCount = "infinite"/>

9. enum:枚举值。

(1)属性定义:

(2)属性使用:

xmlns:android = "http://schemas.android.com/apk/res/android"

android:orientation = "vertical"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

>

10. flag:位或运算。

(1)属性定义:

(2)属性使用:

android:name = ".StyleAndThemeActivity"

android:label = "@string/app_name"

android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">

注意:

属性定义时可以指定多种类型值。

(1)属性定义:

(2)属性使用:

android:layout_width = "42dip"

android:layout_height = "42dip"

android:background = "@drawable/图片ID|#00FF00"

/>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值