1. custom tag: This is my template project directory.
If you want to use your own tag as android: text, you must first have a tag. In res/values/, I created a new mm_tag.xml file.(Do not use uppercase letters, numbers, or underscores.) Step 1: Customize the label mm_tag.xml
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="GridItem"> <attr name="bkground" format="reference|color"/> <attr name="text1" format="string"/> <attr name="text2" format="string"/> <attr name="image" format="reference|integer"/> </declare-styleable> </resources>
Format reference:
1. reference: refer to a resource ID
2. color: color value
3. boolean: boolean Value
4. dimension: dimension value
5. float: floating point value
6. integer: integer value
7. string: string
8. fraction: Percentage
9. enum: enumeration Value
// Attribute definition: <declare-styleable name = "name"> <attr name = "orientation"> <enum name = "horizontal" value = "0"/> <enum name = "vertical" value = "1"/> </attr> </declare-styleable> // attribute usage: <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </LinearLayout>
10. flag: bitwise OR operation
// Attribute definition: <declare-styleable name = "name"> <attr name = "windowSoftInputMode"> <flag name = "stateUnspecified" value = "0"/> <flag name = "stateUnchanged" value = "1"/> <flag name = "stateHidden" value = "2"/> </attr> </declare-styleable> // attribute usage: <activity android: name = ". styleAndThemeActivity "android: label =" @ string/app_name "android: windowSoftInputMode =" stateUnspecified | stateUnchanged | stateHidden "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity>
11. Note: multiple types of values can be specified during attribute definition.
// Attribute definition: <declare-styleable name = "name"> <attr name = "background" format = "reference | color"/> </declare-styleable> // attribute usage: <ImageView android: layout_width = "42dip" android: layout_height = "42dip" android: background = "@ drawable/image ID | #00FF00"/>
Step 2: Get the data returned by the tag in the Custom componentFor example, we use the custom component GridItem in the layout: first declare the namespace of the label.
Xmlns: griditem = "http://schemas.android.com/apk/res/com.mm.template" // android namespace: xmlns: android = "http://schemas.android.com/apk/res/android"
It is found that only res/is different,
Com. mm. templateIs the name of my application package.It can be seen that griditem is the name of a namespace I just want. The next step is to use custom components
<Com. mm. template. gridItem griditem: image = "@ drawable/mm_1" android: padding = "5dp" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" griditem: bkground = "@ color/orange" griditem: text1 = "Android" griditem: text2 = "mobile development"/>
Here, our custom tags are used:
Griditem: image = "@ drawable/mm_1" griditem: bkground = "@ color/orange" griditem: text1 = "Android" griditem: text2 = "mobile development"
How can I get the data returned by the tag? Use the following method in the implementation code of the custom component GridItem:
Public GridItem (Context context, AttributeSet attrs) {super (context, attrs); TypedArray typedarray = context. obtainStyledAttributes (attrs, R. styleable. gridItem); bk_color = typedarray. getResourceId (R. styleable. gridItem_bkground, R. color. burlywood); text1 = typedarray. getString (R. styleable. gridItem_text1); text2 = typedarray. getString (R. styleable. gridItem_text2); image = typedarray. getDrawable (R. styleable. gridItem_image); typedarray. recycle (); view = LayoutInflater. from (context ). inflate (R. layout. mm_grid_item, this, true); layout = (LinearLayout) view. findViewById (R. id. item_layout); textview1 = (TextView) view. findViewById (R. id. text1); textview2 = (TextView) view. findViewById (R. id. text2); imageview = (ImageView) view. findViewById (R. id. imageview); layout. setBackgroundResource (bk_color); // sets the background color textview1.setText (text1); // sets the first line of text textview2.setText (text2); // sets the second line of text imageview. setImageDrawable (image); // set the icon}
You can obtain the data from custom tags and display the data correctly on the interface. Next I will talk about it in combination with the custom component GridItem. 2. I want to implement a custom component. There are many methods like this. You just need to customize the layout, now I want it to be used directly in the layout in the form of components like TextView,
The custom component is used. Below I will implement a custom component GridItem implementation.
Generally, it is inherited from Layout (a problem occurs when I inherit from View ~~!)GridItem. java
Package com. mm. template; import android. content. context; import android. content. res. typedArray; import android. graphics. drawable. drawable; import android. util. attributeSet; import android. view. layoutInflater; import android. view. view; import android. widget. imageView; import android. widget. linearLayout; import android. widget. textView; public class GridItem extends LinearLayout {privateint bk_color; // background color private String text1; // The first line of text private String text2; // The second line of text private Drawable image; // icon private LinearLayout layout; private TextView textview1; private TextView textview2; private ImageView imageview; private View view; public GridItem (Context context, AttributeSet attrs) {super (context, attrs ); typedArray typedarray = context. obtainStyledAttributes (attrs, R. styleable. gridItem); bk_color = typedarray. getResourceId (R. styleable. gridItem_bkground, R. color. burlywood); text1 = typedarray. getString (R. styleable. gridItem_text1); text2 = typedarray. getString (R. styleable. gridItem_text2); image = typedarray. getDrawable (R. styleable. gridItem_image); typedarray. recycle (); view = LayoutInflater. from (context ). inflate (R. layout. mm_grid_item, this, true); layout = (LinearLayout) view. findViewById (R. id. item_layout); textview1 = (TextView) view. findViewById (R. id. text1); textview2 = (TextView) view. findViewById (R. id. text2); imageview = (ImageView) view. findViewById (R. id. imageview); layout. setBackgroundResource (bk_color); // sets the background color textview1.setText (text1); // sets the first line of text textview2.setText (text2); // sets the second line of text imageview. setImageDrawable (image); // set the icon }}
Mm_grid_item.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/item_layout" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: background = "@ color/black" android: padding = "3dp" android: paddingLeft = "6dp" tools: ignore = "HardcodedText, ContentDescrip Tion "> <TextView android: id =" @ + id/text1 "android: layout_weight =" 1 "style =" @ style/MM_TextView "/> <TextView android: id = "@ + id/text2" android: layout_weight = "1" android: textSize = "12sp" style = "@ style/MM_TextView"/> <ImageView android: id = "@ + id/imageview" android: layout_width = "wrap_content" android: layout_height = "0dp" android: layout_gravity = "right" android: src = "@ drawable/ Mm_title_1 "android: layout_weight =" 2 "android: layout_marginTop =" 10dp "android: scaleType =" fitCenter "/> <! -- Image scaling android: scaleX = "0.8" android: scaleY = "0.8" --> </LinearLayout>
3.
Main_layout.xml(My main layout file)
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" xmlns: griditem = "http://schemas.android.com/apk/res/com.mm.template" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "@ color/white" android: orientation = "vertical" tools: ignore = "HardcodedText, ContentDescription, NestedWeig Hts "> <! -- Head start --> <LinearLayout android: layout_width = "match_parent" android: layout_height = "44dp" android: orientation = "horizontal" android: padding = "10dp" android: background = "@ color/red"> <ImageView android: layout_width = "wrap_content" android: layout_height = "match_parent" android: src = "@ drawable/mm_title_1"/> <TextView android: layout_width = "0dp" android: layout_height = "match_p Arent "android: layout_weight =" 1 "android: gravity =" center "android: text =" Test Case "android: textStyle =" bold "android: textSize =" 16sp "android: textColor = "@ android: color/white"/> <ImageView android: layout_width = "wrap_content" android: layout_height = "match_parent" android: src = "@ drawable/mm_title_2"/> </LinearLayout> <! -- Head end --> <! -- Search start --> <LinearLayout android: layout_width = "match_parent" android: layout_height = "36dp" android: orientation = "vertical" android: paddingTop = "3dp" android: layout_margin = "8dp"> <EditText android: id = "@ + id/search_edit" android: layout_width = "match_parent" android: layout_height = "match_parent" android: drawableLeft = "@ drawable/mm_search" android: background = "@ drawable/mm_shap E_editview "android: hint =" Enter the keyword "android: textSize =" 16sp "android: textColorHint =" @ color/darkgray "android: textStyle =" bold "android: padding = "6dp"/> </LinearLayout> <! -- Search end --> <! -- GridItem start --> <LinearLayout android: layout_width = "match_parent" android: layout_height = "0dp" android: layout_weight = "1" android: orientation = "horizontal" android: layout_margin = "10dp"> <com. mm. template. gridItem griditem: image = "@ drawable/mm_1" android: padding = "5dp" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" griditem: B Kground = "@ color/orange" griditem: text1 = "Android" griditem: text2 = "mobile development"/> <com. mm. template. gridItem griditem: image = "@ drawable/mm_2" android: padding = "5dp" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" griditem: bkground = "@ color/bluevilet" griditem: text1 = "C ++" griditem: text2 = "Programming Language"/> <com. mm. template. gridItem griditem: imag E = "@ drawable/mm_3" android: padding = "5dp" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" griditem: bkground = "@ color/blue" griditem: text1 = "server" griditem: text2 = "Background Development"/> </LinearLayout> <! -- GridItem end --> </LinearLayout>
That is, the <com/> label is the custom GridItem component.4. Result Display reference
Note: Please indicate the source for reprinting. After all, the code is coded one by one ~