Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the xml property in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report