http://blog.csdn.net/lihenair/article/details/41009711
Custom controls are often required at work, except for keystrokes and draw, which require some initialization of the control's properties, such as margins, font size, color, and so on.
This article will implement a custom TextView based on the requirements.
1 Requirements
The font required for TextView is 30SP and the color is #ff00ffad.
For these two requirements, make the following definitions
Colors.xml
[HTML]View PlainCopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <resources>
- <color name="TextColor"> #FF00FFAD</color>
- </Resources>
Dimens.xml
[HTML]View PlainCopy
- <resources>
- <!--Default screen margins, per the Android Design guidelines.
- <dimen name="Activity_horizontal_margin">16dp</dimen>
- <dimen name="Activity_vertical_margin">16dp</dimen>
- <dimen name="text_size">30sp</dimen>
- </Resources>
2 Defining attribute declarations
[HTML]View PlainCopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <resources>
- <declare-styleable name="Largetext">
- <attr name="textsize" format="Dimension" />
- <attr name="TextColor" format="color" />
- </declare-styleable>
- </Resources>
It is important to note that the definition of the Format property, format= "Reference", is typically used for strings, which means that a reference to a string is defined
3 Importing controls
With the above definition, we can add the above defined properties to the layout file.
Main.xml
[HTML]View PlainCopy
- <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="Http://schemas.android.com/tools"
- xmlns:largetext="http://schemas.android.com/apk/res/com.example.nfctest"//style of introducing custom attributes
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingbottom="@dimen/activity_vertical_margin"
- android:paddingleft="@dimen/activity_horizontal_margin"
- android:paddingright="@dimen/activity_horizontal_margin"
- android:paddingtop="@dimen/activity_vertical_margin"
- tools:context=". Nfcactivity " >
- <com.example.nfctest.LargeText android:id="@+id/state"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world"
- largetext:textsize="@dimen/text_size"//reference custom font size
- largetext:textcolor="@color/textcolor" />//reference Custom colors
- </relativelayout>
Introduce custom controls to include PackageName in layout, format <package-name>.<customize-class_name>
The custom attribute style needs to be loaded in the layout or view's property column, in the format xmlns:<style-name>= "Http://schemas. android.com/apk/res/<package-name> "
The format for using custom attributes is <style-name>:<attrs-name>
4 Everything is ready, now you need to define Largetext.java
Largetext.java
[Java]View PlainCopy
- Package com.example.nfctest;
- Import Android.content.Context;
- Import Android.content.res.TypedArray;
- Import Android.util.AttributeSet;
- Import Android.widget.TextView;
- Public class Largetext extends TextView {
- Public Largetext (context context) {
- This (context, null, 0);
- //TODO auto-generated constructor stub
- }
- Public Largetext (context context, AttributeSet attrs) {
- This (context, Attrs, 0);
- //TODO auto-generated constructor stub
- }
- Public Largetext (context context, AttributeSet attrs, int defstyle) {
- Super (context, attrs, Defstyle);
- //TODO auto-generated constructor stub
- TypedArray a = Context.obtainstyledattributes (Attrs, R.styleable.largetext, Defstyle, 0);
- int textcolor = A.getcolor (R.styleable.largetext_textcolor,
- 0XFFFFFFFF);
- float textSize = a.getdimension (r.styleable.largetext_textsize, 36);
- A.recycle (); //Using an array of types, you need to recycle it
- Settextsize (textSize);
- SetTextColor (TextColor);
- }
- }
With the 4 steps above, the custom TextView will display the text according to the attributes we define.
How to use xmlns in Android