200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > Android 自定义控件属性

Android 自定义控件属性

时间:2019-12-22 01:11:28

相关推荐

Android 自定义控件属性

前言

自定义控件经常需要一些特殊的配置,添加一些自定义属性。

1. 自定义属性

attrs.xml文件,所有自定义属性需要在文件中添加declare-styleable节点来声明,例如定义属性background_color设置背景色。

<declare-styleable name="AttrDeclareView"><attr name="background_color" format="color" /></declare-styleable>

自定义控件AttrDeclareView继承View,使用Context.obtainStyledAttributes(AttributeSet, int[])方法来解析自定义属性,得到自定义属性background_color的值,调用TypedArray.recycle()方法释放资源,最后设置背景色。

public class AttrDeclareView extends View {public AttrDeclareView(Context context) {this(context, null);}public AttrDeclareView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public AttrDeclareView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AttrDeclareView);int color = a.getColor(R.styleable.AttrDeclareView_background_color, 0);a.recycle();if (color != 0) {setBackgroundColor(color);}}}

布局文件分别引用不同的背景色值。添加命名空间xmlns:app="/apk/res-auto"。引用自定义属性时,使用命名空间+属性名的方式。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><com.blog.demo.custom.widget.AttrDeclareViewandroid:layout_width="match_parent"android:layout_height="50dp"app:background_color="#ff00ff00"/><com.blog.demo.custom.widget.AttrDeclareViewandroid:layout_width="match_parent"android:layout_height="50dp"app:background_color="#ffff8800" /><com.blog.demo.custom.widget.AttrDeclareViewandroid:layout_width="match_parent"android:layout_height="50dp"app:background_color="@color/red" /></LinearLayout>

效果如下

2. 不同控件使用同一属性

不同控件使用相同的自定义属性名时,两者会有冲突,需要将属性名提取到外面进行声明。

<attr name="indicator_color" format="color" /><declare-styleable name="AttrDeclareView"><attr name="background_color" format="color" /><attr name="indicator_color" /></declare-styleable>

3. 使用系统属性

attrs.xml文件,我们需要在AttrDeclareView中需要设置字符,同时设置字符的颜色和字体大小。

<declare-styleable name="AttrDeclareView"><attr name="background_color" format="color" /><attr name="indicator_color" /><attr name="android:text" format="string" /><attr name="android:textSize" format="dimension" /><attr name="android:textColor" format="color" /></declare-styleable>

自定义控件AttrDeclareView,获取自定义文本数据,并在onDraw(Canvas)方法中调用。

public class AttrDeclareView extends View {private String mText;private Paint mPaint;public AttrDeclareView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AttrDeclareView);int color = a.getColor(R.styleable.AttrDeclareView_background_color, 0);mText = a.getString(R.styleable.AttrDeclareView_android_text);int textSize = a.getDimensionPixelSize(R.styleable.AttrDeclareView_android_textSize, 0);int textColor = a.getColor(R.styleable.AttrDeclareView_android_textColor, 0);a.recycle();if (color != 0) {setBackgroundColor(color);}if (mText != null) {mPaint = new Paint();mPaint.setColor(textColor);mPaint.setTextSize(textSize);}}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (mText != null) {canvas.drawText(mText, 0, getHeight()/2, mPaint);}}}

布局文件

<com.blog.demo.custom.widget.AttrDeclareViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="This is a string"android:textSize="12sp"android:textColor="#FF000000"app:background_color="#ff0000ff"/>

效果如下

相关文章

Android 自定义控件属性

Android 自定义控件属性format详解

Android 自定义控件属性赋值

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。