Android defines the use of tags to achieve a flat UI design

Source: Internet
Author: User

August 6, 2014 11:06:44

The use of Android to define its own label. Implement a flat UI design:
Define your own label in the 1.attrs.xml file
Such as:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>


<declare-styleable name= "First" >
Reference refers to a quote from String.xml.


<attr name= "name" format= "Reference"/>
<attr name= "Age" >
Flag is custom, similar to android:gravity= "top"
<flag name= "Child" value= "ten" ></flag>
<flag name= "Yong" value= "></flag>"
<flag name= "Oldman" value= "></flag>"
</attr>
Dimension refers to the content that is quoted from the Dimension.xml.
<attr name= "Testsize" format= "Dimension" ></attr>
</declare-styleable>


</resources>
2. Use your own definition label in the layout file. Assigning values to tag properties
Such as:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Com.example.declarestyleabletest the root path of the package in the manifest file xmlns:test= "http://schemas.android.com/apk/res/ Com.example.declarestyleabletest "
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
MyTest See below
<com.example.declarestyleabletest.mytest
Android:id= "@+id/tvtest"
Test:name= "@string/name"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/test"
Test:age= "Oldman"
Test:testsize= "@dimen/size"/>


</LinearLayout>
3.mytest.java: In the class can get the value assigned to the property in the layout file, after the value can be assigned to the TextView properties of the change style
Such as:
Package com.example.declarestyleabletest;
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.util.AttributeSet;
Import Android.widget.TextView;


public class MyTest extends TextView {


Public MyTest (context context, AttributeSet Attrs) {
Super (context, attrs);


TypedArray Tarray = context.obtainstyledattributes (Attrs,
R.styleable.first);
String name = tarray.getstring (r.styleable.first_name);
int age = Tarray.getint (r.styleable.first_age, 100);
int dimen = Tarray.getint (r.styleable.first_testsize, 110);
System.out.println ("Name:" + name + "Age:" + Age + "dimen:");
Multiplexing Styledattributes
Tarray.recycle ();

}
}
4. Use the changed-style view object in Main.activity through Id:tvtest, such as TextView in this example.

The following is attached to the two demo, the first for the simple self-defined label assignment and extraction, the second for the more complex Flatui flat UI implementation. We can take a look at it.

Flattening:


http://download.csdn.net/detail/u014071669/7720493

Android defines the use of tags to achieve a flat UI design

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.