Android 自定义控件属性
2018-04-08 本文已影响0人
十二书
1. 自定义一个CustomView(extends View )类
2. 编写values/attrs.xml,在其中编写styleable和item等标签元素
<!-- 一般写在attrs.xml文件中 -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="test"> <!-- name 可以随便写,但通常会与自定义控件类名相同,这样更直观 -->
<attr name="text" format="string" />
<attr name="testAttr" format="integer" />
</declare-styleable>
</resources>
3. 在布局文件中CustomView使用自定义的属性(注意namespace)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.test.MyTextView
android:layout_width="100dp"
android:layout_height="200dp"
app:testAttr="520"
app:text="helloworld" />
</RelativeLayout>
4. 在CustomView的构造方法中通过TypedArray获取
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test);
String text = ta.getString(R.styleable.test_testAttr);
int textAttr = ta.getInteger(R.styleable.test_text, -1);
Log.e(TAG, "text = " + text + " , textAttr = " + textAttr);
ta.recycle();
}
不过,作为模板代码,我觉得CirclePageIndicator的写法更好。
public CirclePageIndicator(Context context) {
this(context, (AttributeSet)null);
}
public CirclePageIndicator(Context context, AttributeSet attrs) {
this(context, attrs, attr.vpiCirclePageIndicatorStyle);
}
public CirclePageIndicator(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mPaintPageFill = new Paint(1);
this.mPaintStroke = new Paint(1);
this.mPaintFill = new Paint(1);
this.mLastMotionX = -1.0F;
this.mActivePointerId = -1;
if (!this.isInEditMode()) {
// 先获取默认值
Resources res = this.getResources();
int defaultPageColor = res.getColor(color.default_circle_indicator_page_color);
int defaultFillColor = Color.parseColor("#FF3964");
int defaultOrientation = res.getInteger(integer.default_circle_indicator_orientation);
int defaultStrokeColor = res.getColor(color.default_circle_indicator_stroke_color);
float defaultStrokeWidth = res.getDimension(dimen.default_circle_indicator_stroke_width);
float defaultRadius = res.getDimension(dimen.default_circle_indicator_radius);
boolean defaultCentered = res.getBoolean(bool.default_circle_indicator_centered);
boolean defaultSnap = res.getBoolean(bool.default_circle_indicator_snap);
// 然后获取自定义属性在XML中的设置值
TypedArray a = context.obtainStyledAttributes(attrs, styleable.CirclePageIndicator, defStyle, 0);
this.mCentered = a.getBoolean(styleable.CirclePageIndicator_centered, defaultCentered);
this.mOrientation = a.getInt(styleable.CirclePageIndicator_android_orientation, defaultOrientation);
this.mPaintPageFill.setStyle(Style.FILL);
this.mPaintPageFill.setColor(a.getColor(styleable.CirclePageIndicator_pageColor, defaultPageColor));
this.mPaintStroke.setStyle(Style.STROKE);
this.mPaintStroke.setColor(a.getColor(styleable.CirclePageIndicator_strokeColor, defaultStrokeColor));
this.mPaintStroke.setStrokeWidth(a.getDimension(styleable.CirclePageIndicator_strokeWidth, defaultStrokeWidth));
this.mPaintFill.setStyle(Style.FILL);
this.mPaintFill.setColor(a.getColor(styleable.CirclePageIndicator_fillColor, defaultFillColor));
this.mRadius = a.getDimension(styleable.CirclePageIndicator_radius, defaultRadius);
this.mSnap = a.getBoolean(styleable.CirclePageIndicator_snap, defaultSnap);
Drawable background = a.getDrawable(styleable.CirclePageIndicator_android_background);
if (background != null) {
this.setBackgroundDrawable(background);
}
// recycle
a.recycle();
ViewConfiguration configuration = ViewConfiguration.get(context);
this.mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
}
}
5. 在style文件中使用自定义属性
https://blog.csdn.net/jiahongyu0529/article/details/78701490
https://blog.csdn.net/tomlucky1024/article/details/60877926
不需要加限定,直接使用
<style name="ScanUploading">
<item name="android:background">@color/white</item>
<item name="testAttr">"how do u do"</item>
</style>