How to use xmlns in Android

Source: Internet
Author: User

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 
    1. <? XML version= "1.0" encoding="Utf-8"?>
    2. <resources>
    3. <color name="TextColor"> #FF00FFAD</color>
    4. </Resources>

Dimens.xml

[HTML]View PlainCopy 
  1. <resources>
  2. <!--Default screen margins, per the Android Design guidelines.
  3. <dimen name="Activity_horizontal_margin">16dp</dimen>
  4. <dimen name="Activity_vertical_margin">16dp</dimen>
  5. <dimen name="text_size">30sp</dimen>
  6. </Resources>


2 Defining attribute declarations

[HTML]View PlainCopy 
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <resources>
  3. <declare-styleable name="Largetext">
  4. <attr name="textsize" format="Dimension" />
  5. <attr name="TextColor" format="color" />
  6. </declare-styleable>
  7. </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 
  1. <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="Http://schemas.android.com/tools"
  3. xmlns:largetext="http://schemas.android.com/apk/res/com.example.nfctest"//style of introducing custom attributes
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingbottom="@dimen/activity_vertical_margin"
  7. android:paddingleft="@dimen/activity_horizontal_margin"
  8. android:paddingright="@dimen/activity_horizontal_margin"
  9. android:paddingtop="@dimen/activity_vertical_margin"
  10. tools:context=". Nfcactivity " >
  11. <com.example.nfctest.LargeText android:id="@+id/state"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="@string/hello_world"
  15. largetext:textsize="@dimen/text_size"//reference custom font size
  16. largetext:textcolor="@color/textcolor" />//reference Custom colors
  17. </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 
  1. Package com.example.nfctest;
  2. Import Android.content.Context;
  3. Import Android.content.res.TypedArray;
  4. Import Android.util.AttributeSet;
  5. Import Android.widget.TextView;
  6. Public class Largetext extends TextView {
  7. Public Largetext (context context) {
  8. This (context, null, 0);
  9. //TODO auto-generated constructor stub
  10. }
  11. Public Largetext (context context, AttributeSet attrs) {
  12. This (context, Attrs, 0);
  13. //TODO auto-generated constructor stub
  14. }
  15. Public Largetext (context context, AttributeSet attrs, int defstyle) {
  16. Super (context, attrs, Defstyle);
  17. //TODO auto-generated constructor stub
  18. TypedArray a = Context.obtainstyledattributes (Attrs, R.styleable.largetext, Defstyle, 0);
  19. int textcolor = A.getcolor (R.styleable.largetext_textcolor,
  20. 0XFFFFFFFF);
  21. float textSize = a.getdimension (r.styleable.largetext_textsize, 36);
  22. A.recycle (); //Using an array of types, you need to recycle it
  23. Settextsize (textSize);
  24. SetTextColor (TextColor);
  25. }
  26. }


With the 4 steps above, the custom TextView will display the text according to the attributes we define.



How to use xmlns in Android

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.