Use of typedarray Custom Attributes

Source: Internet
Author: User

Connection: http://w11h22j33.iteye.com/blog/890410

Today, the source code of alertdialog has theme. I want to modify the background and topic of alertdialog. I can see a post about how to modify the background of alertdialog, many classes do not know. typedarray is one of them.

So I found some information on the Internet.

We usually define control attributes in XML files, such as Android: Text Android: size, which can be customized.

The answer is yes. Android is still user-friendly, and can customize attributes, implementation styles, themes, and so on.

First, create an attrs. xml file under the values file:

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>

<Declare-styleable name = "myview">
<ATTR name = "textcolor" format = "color"/>
<ATTR name = "textsize" format = "dimension"/>
</Declare-styleable>
</Resources>

<Declare-styleable> It is a custom tag.

I can't do anything else.

Then use custom properties to set the view

Package cn.edu. WTU;

Import Android. content. context;
Import Android. content. res. typedarray;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. Graphics. Paint. style;
Import Android. util. attributeset;
Import Android. View. view;

Public class myview extends view {

Private paint mpaint;
Private context mcontext;
Private Static final string mstr = "Welcome to typed-array ";
 
Public myview (context ){
Super (context );
Mpaint = new paint ();
}
Public myview (context, attributeset attrs ){
Super (context, attrs );
// Todo auto-generated constructor stub
Mpaint = new paint ();
Typedarray A = context. obtainstyledattributes (attrs, R. styleable. myview );
// Obtain the property. 0 xffffffff 16 is the default value.
Int textcolor = A. getcolor (R. styleable. myview_textcolor, 0 xffffffff );
Float textsize = A. getdimension (R. styleable. myview_textsize, 16 );
Mpaint. settextsize (textsize );
Mpaint. setcolor (textcolor );
// Recycling, for future use
A. Recycle ();
}
 
Protected void ondraw (canvas ){
Super. ondraw (canvas );
Mpaint. setstyle (style. Fill );
Canvas. drawrect (10, 10,100,100, mpaint );
Mpaint. setcolor (color. Blue );
Canvas. drawtext (mstr, 10,110, mpaint );
}
}
 

Note the names of R. styleable. myview, R. styleable. myview_textcolor.

Use the myview control in the main. xml file.

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Xmlns: test = "http://schemas.android.com/apk/res/cn.edu.wtu"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Cn.edu. WTU. myview Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Test: textsize = "20sp"
Test: textcolor = "# ffffffff"
/>

</Linearlayout>
PS: xmlns: test = "http://schemas.android.com/apk/res/cn.edu.wtu"
Cn.edu. WTU is my package name and the domain name of my school

Test is the name. Test: textcolor = "# ffffffff"
 

Package cn.edu. WTU;

Import Android. App. activity;
Import Android. App. Dialog;
Import Android. OS. Bundle;

Public class alert extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.