Android custom View (1), androidview

Source: Internet
Author: User

Android custom View (1), androidview

Recently I was writing an application about music playback. When I wrote it to the UI of the playback interface, I wanted to implement a playback interface myself. So how to implement custom View? By viewing others' blogs and official Android development documents, we have a preliminary understanding of some simple content. This record is provided for reference by friends who need it.

Official reference for custom views: http://developer.android.com/training/custom-views/create-view.html

When we use an XML layout file for UI writing, by adding an xml tag, we can add any view components we want, such as the layout file below (just an example, other unimportant layout elements are deleted ):

1 <?xml version="1.0" encoding="utf-8"?>2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"3     android:layout_width="match_parent"4     android:layout_height="match_parent">5     <TextView6         android:layout_width="match_parent"7         android:layout_height="match_parent" />8 </RelativeLayout>

You have used <RelativeLayout to introduce RelativeLayout and <TextView to introduce TextView. Then, various attributes (such as android: layout_width and android: layout_heignt) are added to the corresponding tab. The compiling program parses the xml file during compilation, generate a view object for the corresponding tag.

So how can I use a custom view in the above method? This is about xmlns (XML namespace. Anyone who knows about XML knows that XML is designed with eXtensible Markup Language for data transmission. It is different from HTML with predefined tabs. Its tabs are user-defined. Where is the definition? Xmlns!

I don't know if you have noticed that this line of code is always used in our layout file:

xmlns:android="http://schemas.android.com/apk/res/android"

This is the introduction of namespace. For more information about XML, see XML introduction. Simply put, by introducing a namespace to a top-level tag, the entire xml file can use the content defined in the namespace, such as android: id. So how can we add namespaces for our own views?

Next we will create our own namespace file: first, create an attrs resource file under the res-> values directory, such:

Attributes to be expressed in attrs definition:

1 <?xml version="1.0" encoding="utf-8"?>2 <resources>3     <declare-styleable name="Circle">4         <attr name="radius" format="dimension"/>5         <attr name="background_color" format="color"/>6     </declare-styleable>7 </resources>

The value of the name attribute of the declare-styeable node is generally the name of the view you wrote. For example, the view I wrote here is Cirecle. Next we will define the component attributes that can be defined in xml. Here we can specify two, radius and background. The format attribute specifies the type of acceptable values. Multiple types are separated by "|.

Related Article

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.