Or the problem in the previous example, what if you want to set the radius of the sphere in XML? Let's look at the knowledge of custom attributes.
One, the format in the properties file
First we want to see if there is a attrs.xml in the values directory, if there is no one to create.
Format can be selected
Reference//reference
Color
Boolean
Dimension/Size
Float
Integer
String
Fraction//percentage, e.g. 200%
Here you can customize several properties, in the Attrs.xml file, as follows
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "Ballview" > <attr name= "Ballcolor" format= "color"/> <attr name= "Ballradius" format= "float"/> <attr Name= "Ballstartx" format= "float"/> <attr name= "Ballstarty" format= "float"/> </ Declare-styleable></resources>
Where Ballview is used to find this data in Java code.
The layout file is as follows
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " android:orientation=" vertical "android:layout_width=" match_parent " android:layout_height=" Match_parent "> <chuiyuan.lsj.androidjava.utils.ballview2 xmlns:ballview="/http Schemas.android.com/apk/res-auto " android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " ballview:ballradius=" /></linearlayout>
Note the xmlns in BallView2, using Res-auto.
Ii. examples
Next is the code for BALLVIEW2, the constructor is the focus, we get the defined properties, and the Get method usually sets the default value, so that we don't have a definition in the XML file. Gets the property that is concatenated by using the name _ property. Typedarray usually finally calls the Recycle () method to maintain consistency when using properties later.
Package Chuiyuan.lsj.androidjava.utils;import Android.content.context;import Android.content.res.TypedArray; Import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import chuiyuan.lsj.androidjava.r;/** * Created by LSJ on 2015/9/26.e */public class BallView2 extends view{private float x ; private float y; private float R; private int color; Public BallView2 (Context context) {Super (context, NULL); } public BallView2 (context context, AttributeSet Attrs) {Super (context, attrs); Get the custom properties TypedArray ta = context.obtainstyledattributes (attrs, R.styleable.ballview); When this attribute is not defined in XML, the default value of x = Ta.getfloat (r.styleable.ballview_ballstartx,30) is used; y = ta.getfloat (r.styleable.ballview_ballstarty,30); R = ta.getfloat (r.styleable.ballview_ballradius,30); color = Ta.getcolor (r.styleable. Ballview_ballcolor, Color.green); Ta.recycle (); Be sure to} @Override public void OnDraw (canvas canvas) {Paint paint = new Paint (); Paint.setcolor (color); Canvas.drawcircle (x, Y, R, Paint); } @Override public boolean ontouchevent (Motionevent event) {x = Event.getx (); y = event.gety (); Invalidate (); Refresh return true; Success}}
End..
Add custom properties for Android custom Controls-(2)