Android custom property step, android custom step

Source: Internet
Author: User
Tags getcolor

Android custom property step, android custom step

Whether a program can attract users, beautiful UI and excellent interaction are crucial factors. Therefore, most applications do not meet the requirements of the UI components provided by the system, but use custom components to achieve better display. Custom Attributes are used in most cases when custom components are used. This document records several steps for custom attributes:

1. Plan the attribute names and types you need to define.

2. Create an attrs. xml file under the res/values directory, and define the previously planned attributes in attrs. xml. The details are as follows:

1 <declare-styleable name="MyTextView">2     <attr name="textColor" format="color"/>3     <attr name="textSize" format="dimension"/>4     <attr name="text" format="string"/>5     <attr name="background" format="reference|color"/>6 </declare-styleable>

3. After the property is defined, you must obtain the value of the custom property in the Custom View in the constructor to implement the custom view. The custom attributes are referenced by R. styleable. To obtain the attributes, you must use the "name_attribute" method. After TypeArray is used, perform recycle () as follows:

 1 public MyTextView(Context context, AttributeSet attrs) { 2         super(context, attrs); 3         mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); 4         TypedArray typeArray = context 5                 .obtainStyledAttributes(attrs, R.styleable.MyTextView); 6  7         mTextColor = typeArray.getColor(R.styleable.MyTextView_textColor, Color.BLACK); 8         mTextSize = typeArray.getDimension(R.styleable.MyTextView_textSize, 14); 9         mText = typeArray.getString(R.styleable.MyTextView_text);10         11         mTextBackground = typeArray.getColor(R.styleable.MyTextView_background, Color.WHITE);12         13         mTextPaint.setColor(mTextColor);14         mTextPaint.setTextSize(mTextSize);15         mTextPaint.setTypeface(Typeface.DEFAULT);16         17         typeArray.recycle();18     }

4. Implement the custom view, and then use the custom view to set the custom view value in xml. As follows:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     xmlns:mytextview = "http://schemas.android.com/apk/res/com.example.viewdemo" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent" 6     android:orientation="vertical" > 7      8     <com.example.view.textview.MyTextView  9         android:layout_width="500dp"10         android:layout_height="wrap_content"11         mytextview:text="my name is text view , i am a text view"12         mytextview:textSize="20sp"13         mytextview:textColor="#000000"14         mytextview:background="#ffffff"15         />16 17 </LinearLayout>

Pay special attention to the statement marked with red. You need to set xmlns to reference the custom attributes defined above. The namespace is "mytextview" and the value is "http://schemas.android.com/apk/res/???package name =com.example.viewdemo= ". Then, the following custom view tag uses the namespace: attribute name to assign values to the custom attribute.

In this way, our custom attributes are completed, and you can get and use the attribute values in the Custom view.

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.