在xml 文件里定义控件的属性,我们已经习惯了android:attrs="" ,那么我们能不能定义自己的属性能,比如:test:attrs="" 呢?答案是肯定的.
好了我就不卖关子了,直接进入主题。大致以下步骤:
一、 在res/values 文件下定义一个attrs.xml 文件.代码如下:
view plaincopy to clipboardprint?
一、在res/values文件下定义一个attrs.xml文件.代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
二、 我们在MyView.java 代码修改如下,其中下面的构造方法是重点,我们获取定义的属性我们R.sytleable.MyView_textColor, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ), 防止我们在xml 文件中没有定义.从而使用默认值!
获取,MyView 就是定义在<declare-styleable name="MyView "></declare-styleable> 里的 名字,获取里面属性用 名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!
view plaincopy to clipboardprint?
public MyView(Context context,AttributeSet attrs)
{
super(context,attrs);
mPaint = new Paint();
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MyView);
int textColor = a.getColor(R.styleable.MyView_textColor,
0XFFFFFFFF);
float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
mPaint.setTextSize(textSize);
mPaint.setColor(textColor);
a.recycle();
}
MyView.java 全部代码如下:
view plaincopy to clipboardprint?
package com.android.tutor;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
private Paint mPaint;
private Context mContext;
private static final String mString = "Welcome to Mr Wei's blog";
public MyView(Context context) {
super(context);
mPaint = new Paint();
}
public MyView(Context context,AttributeSet attrs)
{
super(context,attrs);
mPaint = new Paint();
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MyView);
int textColor = a.getColor(R.styleable.MyView_textColor,
0XFFFFFFFF);
float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
mPaint.setTextSize(textSize);
mPaint.setColor(textColor);
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//设置填充
mPaint.setStyle(Style.FILL);
//画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);
mPaint.setColor(Color.BLUE);
//绘制文字
canvas.drawText(mString, 10, 110, mPaint);
}
}
三、将我们自定义的MyView 加入布局main.xml 文件中,平且使用自定义属性,自定义属性必须加上:
xmlns:test ="http://schemas.android.com/apk/res/com.android.tutor "蓝色 是自定义属性的前缀,红色 是我们包名.
main.xml 全部代码如下:
view plaincopy to clipboardprint?
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.android.tutor.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
test:textSize="20px"
test:textColor="#fff"
/>
</LinearLayout>
分享到:
相关推荐
测试:Android 中自定义属性(attr.xml,TypedArray)的使用 注意:MyView(Context context,AttributeSet attrs)构造函数的实现, 和注意main.xml的LinearLayout 里加的声明 要了解:test:textSize="20px" test:...
Android自定义View中attrs.xml的实例详解 我们在自定义View的时候通常需要先完成attrs.xml文件 在values中定义一个attrs.xml 然后添加相关属性 这一篇先详细介绍一下attrs.xml的属性。 <?xml version=1.0 ...
Android自定义Attr属性 的具体使用例子,看完之后,你就知道如何自定义控件的属性
Android高手进阶教程之----Android 中自定义属性(attr.xml,TypedArray)的使用! .doc Android高手进阶教程之----Android中万能的BaseAdapter(Spinner,ListView,GridView)的使用!.doc Android高手进阶教程之----通过...
Android自定义聊天气泡形状ImageView可设置箭头位置(左或右),图片圆角大小(属性在自定义attr.xml里面)
自定义属性都存在于/value/attr.xml文件中,以如下格式存在。 代码如下: ”自定义属性名称”> <attr name=”属性名称” format=”属性种类”/> …… 对于自定义属性中的format的值及其含义如下: format属性值:...
第一步:在项目的attr.xml声明自定义属性(各种模式都会用到的属性) Setup 1:declare attribute in attr.xml 第二步:在项目的style.xml指定各种模式主题下的自定义属性值 Setup 2:declare attribute in ...
最近在研究android自定义控件属性,学到了TypedArray以及attrs。大家也可以结合《理解Android中的自定义属性》这篇文章进行学习,后续一篇还有应用。 1、attrs文件编写 <?xml version=1.0 encoding=utf-8?> ...
" "(6)在AndroidManifest.xml中,使用标记的子标记配置Activity的父Activity " "(7)运行实例,查看实验结果。 " "四、程序调试(结果及分析) " " " "五、总结与体会 " "通过本次实验,知道了如何设计一个层级...
* (1)自定义属性:a、创建attr.xml文件 b、在布局中使用 c、在自定义控件中解析使用 * (2)重写onMeasure与onLayout方法,设置菜单按钮的布局位置 * 2、定位Item(设置每一个菜单项的位置) * 3、展开Item(动画...
option自定义属性的用处,可以推广到其他的html标签
css-attr.js
在Android开发中,往往要用到自定义的控件来实现我们的需求或效果。在使用自定义 控件时,难免要用到自定义属性,那怎么使用自定义属性呢? 在文件res/values/下新建attrs.xml属性文件,中定义我们所需要的属性。 ...
此属性在EditView中使用,但是这里也可以用。 android:imeOptions附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号。这个在EditView中再详细...
本文实例分析了Android重写View并自定义属性的方法。分享给大家供大家参考,具体如下: 这里通过自定义属性 实现如下图所示效果: 第一步:在res\values的目录下新建一个文件attrs.xml 声明一些自定义属性 <?...
画雨滴首先我们要知道一点是,所有的雨滴都是随机产生的,而产生的值,可以根据上面的自定义属性指定,也可以使用自定义的值,我们先定义一个雨滴类private class RainDrop{ float speedX; //雨滴x轴移动速度 ...
xml中设置自定义属性 分三步: 1. 在项目中的values文件中创建attrs文件 <?xml version=1.0 encoding=utf-8?> <resources> <declare name=QLoadingIndicatorView> <attr name=indicatorId format=integer...
1.自定义属性attrs.xml <?xml version=1.0 encoding=utf-8?> <attr name=radius format=dimension> <attr name=left_top_radius format=dimension> <attr name=right_top_radius format=d
Android自定义控件属性详细介绍 1. reference:参考某一资源ID。 (1)属性定义: <attr xss=removed xss=removed> (2)属性使用: <ImageView android:layout_width = 42dip android:layout_...