相關文章
Android 自定義控件屬性
Android 自定義控件屬性format詳解
Android 自定義控件屬性賦值
前言
自定義控件經常需要一些特殊的配置,添加一些自定義屬性。
1. 自定義屬性
(1) attrs.xml文件
所有自定義屬性需要在文件中添加declare-styleable
節點來聲明,定義屬性background_color
設置背景色。
<declare-styleable name="AttrDeclareView">
<attr name="background_color" format="color|reference" />
</declare-styleable>
(2) 自定義控件AttrDeclareView
自定義控件繼承View
,使用Context.obtainStyledAttributes
方法來解析自定義屬性,得到自定義屬性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);
}
}
}
(3) 佈局文件
佈局文件分別引用不同的背景色值。添加命名空間xmlns:app="http://schemas.android.com/apk/res-auto"
。引用自定義屬性時,使用命名空間+屬性名的方式。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.blog.demo.custom.view.AttrDeclareView
android:layout_width="match_parent"
android:layout_height="50dp"
app:background_color="#ff00ff00"/>
<com.blog.demo.custom.view.AttrDeclareView
android:layout_width="match_parent"
android:layout_height="50dp"
app:background_color="@android:color/holo_orange_dark" />
<com.blog.demo.custom.view.AttrDeclareView
android:layout_width="match_parent"
android:layout_height="50dp"
app:background_color="@color/red" />
</LinearLayout>
(4) 效果如下
2. 不同控件使用同一屬性
不同控件使用相同的自定義屬性名時,兩者會有衝突,需要將屬性名提取到外面進行聲明。
<attr name="indicator_color" format="color|reference" />
<declare-styleable name="TitleTabView">
<attr name="indicator_width" format="dimension|reference" />
<attr name="indicator_height" format="dimension|reference" />
<attr name="indicator_color" />
<attr name="indicator_radius" format="dimension|reference" />
</declare-styleable>
<declare-styleable name="AttrDeclareView">
<attr name="background_color" format="color|reference" />
<attr name="indicator_color" />
</declare-styleable>
3. 使用系統屬性
(1) attrs.xml文件
我們需要在AttrDeclareView
中需要設置字符,同時設置字符的顏色和字體大小。
<declare-styleable name="AttrDeclareView">
<attr name="background_color" format="color|reference" />
<attr name="indicator_color" />
<attr name="android:text" format="string|reference" />
<attr name="android:textSize" format="dimension|reference" />
<attr name="android:textColor" format="color|reference" />
</declare-styleable>
(2) 自定義控件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);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mText != null) {
canvas.drawText(mText, 0, getHeight()/2, mPaint);
}
}
}
(3) 效果如下