The use of Android custom tags for a flat UI design

Source: Internet
Author: User

August 6, 2014 11:06:44

Android's use of custom tags enables a flat UI design:
Customizing labels in 1.attrs.xml files
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 defined by itself, similar to the 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 custom tags in layout files to assign values to tag properties
Such as:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Com.example.declarestyleabletest is 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 a class you can get the value assigned to a property in a layout file, and you can assign a value to the TextView property to modify the 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 Id:tvtest to modify the style of the View object in main.activity, such as the TextView in this example.

Below is attached two demo, the first for simple custom label assignment and take out, the second for the more complex Flatui flat UI implementation, we can refer to.

Flattening:


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

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.