目录
自定义View
自定义View之Canvas
自定义View之Paint
概述
自定义属性
声明属性
使用declare-styleable声明属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="enableOnPad" format="boolean" /> <attr name="supportDeviceType" format="reference"/> <declare-styleable name="ExTextView"> <attr name="enableOnPad"/> <attr name="supportDeviceType"/> </declare-styleable>
<declare-styleable name="ExEditText"> <attr name="enableOnPad"/> <attr name="supportDeviceType"/> <attr name="line_color" format="color" /> <attr name="line_stroke_height" format="dimension"/> </declare-styleable> </resources>
|
使用属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
xml中使用属性
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"> <CustomView android:layout_width="match_parent" android:layout_height="60dp" app:line_color="" /> </RelativeLayout>
public class CustomView extends View {
public DottedLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.ExEditText); mLineColor = array.getColor(R.styleable.ExEditText_line_color, getResources().getColor(R.color.Red)); mLineStrokeHeight = array.getDimension(R.styleable.ExEditText_line_stroke_height, dp2px(getContext(), 1)); array.recycle(); } }
|