1.attr.xml
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <declare-styleablename= "MyView"> <attrname= "TextColor"format= "Color" /> <attrname= "TextSize"format= "Dimension" /> </declare-styleable></Resources>
2. Using in the construction method
PublicMyView (Context context,attributeset attrs) {Super(Context,attrs); Mpaint=NewPaint (); TypedArray a=context.obtainstyledattributes (Attrs, R.styleable.myview); intTextColor =A.getcolor (R.styleable.myview_textcolor,0XFFFFFFFF); How to obtain: the name _ property can be connected together floatTextSize = A.getdimension (r.styleable.myview_textsize, 36); Mpaint.settextsize (textSize); Mpaint.setcolor (TextColor); A.recycle (); }
3. Using in the layout file
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"xmlns:test= "Http://schemas.android.com/apk/res/com.android.tutor"//Custom attribute prefix and package nameandroid:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" ><com. Android.tutor.MyView android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"test:textsize= "20px"Test:textcolor= "#fff"/></LinearLayout>