1.在resource中定义declare-styleable。
<declare-styleable name="SmileRating"> <attr name="angryColor" format="color" /> <attr name="normalColor" format="color" /> <attr name="drawingColor" format="color" /> <attr name="showLine" format="boolean" /> <attr name="placeHolderSmileColor" format="color" /> <attr name="placeHolderBackgroundColor" format="color" /> <attr name="textSelectionColor" format="color" /> <attr name="textNonSelectionColor" format="color" /> </declare-styleable>2.使用:<com.XXX.XXXView> android:id="@+id/ratingView" android:layout_width="match_parent" android:layout_height="wrap_content" app:angryColor="#fff" android:layout_centerVertical="true" />3.在构造方法中获取属性值(注意getColor的key格式) TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.SmileRating); Color1 = a.getColor(R.styleable.SmileRating_angryColor, defValue); Color2 = a.getColor(R.styleable.SmileRating_normalColor, defValue); Color3 = a.getColor(R.styleable.SmileRating_drawingColor, defValue); a.recycle();