The first method, directly set the property value, through Attrs.getattributeresourcevalue to get this property value.
(1) Setting property values in an XML file
< Com.example.activity.IconTextView Android:layout_width = "Fill_parent" android:layout_height= "Wrap_content" android:text= "@string/smile1" iconsrc= "@drawable/smile"/>
(2) Get this value in the constructor
Public Icontextview (context context, AttributeSet Attrs) { Super (context, attrs); ResourceID = Attrs. Getattributeresourcevalue (NULL, "ICONSRC", 0); if (ResourceID > 0) { bitmap = Bitmapfactory.decoderesource (Getresources (), ResourceID); } }
The second method, use your own namespace
(1) Note in the XML file, you need to declare a namespace in the form of HTTP//+ the package name of the view
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:mobile= "Http://com.example.activity"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <Com.example.activity.IconTextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/smile1"mobile:iconsrc= "@drawable/smile"/> </LinearLayout>
(2) through Attrs.getattributeresourcevalue, where the first parameter is a namespace.
- Name space
Private Final String namespace = "Http://com.example.activity"
Public Icontextview (Context context, AttributeSet attrs) { Super(context, attrs); = Attrs.getattributeresourcevalue (namespace, "Iconsrc", 0); // TypedArray array = context.obtainstyledattributes (Attrs, R.styleable.icontextview); // ResourceID = Array.getresourceid (r.styleable.icontextview_iconsrc, 0); if (ResourceID > 0) { = Bitmapfactory.decoderesource (getresources (), ResourceID); } }
The third Way, by customizing the Attrs.xml, is to implement
(1) Customizing a attrs.xml file
<?XML version= "1.0" encoding= "Utf-8"?> <Resources> <declare-styleablename= "Icontextview"> <attrname= "Iconsrc"format= "Reference"/> </declare-styleable> </Resources>
(2) Use this attribute in an XML file, noting the writing specification of the namespace at this time.
<?XML version= "1.0" encoding= "Utf-8"?> <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:mobile= "Http://schemas.android.com/apk/res/com.example.activity"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <Com.example.activity.IconTextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/smile1"mobile:iconsrc= "@drawable/smile"/> <Com.example.activity.IconTextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/smile2"android:textsize= "24DP"mobile:iconsrc= "@drawable/smile"/> <Com.example.activity.IconTextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/smile3"android:textsize= "36DP"mobile:iconsrc= "@drawable/smile"/> </LinearLayout>
(3) Use Context.obtainstyledattributes in code to get property values
Package com.example.activity; Import Android.content.Context; Import Android.content.res.TypedArray; Import Android.graphics.Bitmap; Import Android.graphics.BitmapFactory; Import Android.graphics.Canvas; Import Android.graphics.Rect; Import Android.util.AttributeSet; Import Android.widget.TextView; public class Icontextview extends TextView {//namespace private final String namespace = "Http://com.example.activit Y "; Resource ID private int resourceID = 0; Private Bitmap Bitmap; Public Icontextview (context context, AttributeSet Attrs) {Super (context, attrs); TypedArray array = context.obtainstyledattributes (Attrs, R.styleable.icontextview); ResourceID = Array.getresourceid (r.styleable.icontextview_iconsrc, 0); if (ResourceID > 0) {bitmap = Bitmapfactory.decoderesource (Getresources (), ResourceID); }} @Override public void OnDraw (canvas canvas) {if (Bitmap! = Null) {rect src = new Rect (0, 0, bitmap.getwidth (), Bitmap.getheight ()); Rect target = new rect (); int textHeight = (int) gettextsize (); Target.left = 0; Target.top = (int) (Getmeasuredheight ()-gettextsize ())/2 + 1; Target.bottom = Target.top + textHeight; Target.right = (int) (TextHeight * (Bitmap.getwidth ()/(float) bitmap.getheight ())); Canvas.drawbitmap (bitmap, SRC, target, getpaint ()); Canvas.translate (target.right + 2, 0); } super.ondraw (canvas); } }
Three ways to get properties in Android custom Controls (GO)