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