How to customize the xml property in Android
Most people do not understand the knowledge points of this article "how to customize xml attributes in Android", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to customize xml attributes in Android" article.
1. First create a new android application.
two。 Create an attribute
Create an attr.xml file under res/values/ and define the required attributes
3. Create a custom View
Create a View that CustomView inherits from View (depending on the situation, if the requirement is not much different from the existing widget or layout, inherit and rewrite some methods)
Package com.hualu.androidview; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class CustomView extends View {private Paint p = null; private String text = null; public CustomView (Context context) {super (context); initCustomView () } public CustomView (Context context, AttributeSet attrs) {super (context, attrs); initCustomView (); TypedArray a = context.obtainStyledAttributes (attrs,R.styleable.custom); int indexCount = a.getIndexCount (); for (int I = 0; I < indexCount; I + +) {int index = a.getIndex (I) Switch (index) {case R.styleable.custom_text: text = a.getString (index); break; case R.styleable.custom_size: p.setTextSize (a.getInt (index, 0)); break Case R.styleable.custom_color: p.setColor (a.getColor (index, 0xFF000000)); break;}} a.recycle ();} void initCustomView () {p = new Paint (); p.setAntiAlias (true);} @ Override protected void onDraw (Canvas canvas) {super.onDraw (canvas); canvas.drawText (text, 10,10, p);}}
4. Use custom view in layout files
The above is about the content of this article on "how to customize xml attributes in Android". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.