I have already written some custom view of the article, here on it does not start from the beginning, such as interested readers can look at the author's first two articles.
Use of Android Custom view (best demo--return title bar)
Android custom Control (bottom icon click Effect)
The author's previous article only described how to use custom view and Why to use a custom view, and so on, but in the actual operation, we still want to customize the view, we can directly in the XML to manipulate it, such as:
So how does it work? Mainly three steps: 1, custom attribute name 2, associate property name with Control 3, get from third-party namespace to custom property name
Main code:
1. Custom attribute Names
First, create an XML file in the values file, and write the name and type of the custom attribute you want.
The code in Atts.xml is as follows:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name="MyTitle"> <attr name="TextColor" format="Color"/> <attr name="TitleText" format="string"/> <attr name="lefttext" format="string"/> <attr name="Righttext" format="string"/> </declare-styleable></Resources>
2. Associating a property name with a control
This point is relatively simple, look directly at the code:
Myview.java
PackageCom.example.double2.viewxmltest;ImportAndroid.content.Context;ImportAndroid.content.res.TypedArray;ImportAndroid.graphics.Color;ImportAndroid.util.AttributeSet;ImportAndroid.view.LayoutInflater;ImportAndroid.widget.LinearLayout;ImportAndroid.widget.TextView;/** * Project name: Viewxmltest * Created by: Double2 No. * Created: 2016/8/4 10:23 * Edit Note: */ Public class MyView extends linearlayout { Private intColortext;PrivateString Textleft;PrivateString Texttitle;PrivateString Textright;PrivateTextView Tvleft;PrivateTextView Tvtitle;PrivateTextView Tvright; Public MyView(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr);//Gets the font color from the XML attribute to the stringTypedArray ta=context.obtainstyledattributes (Attrs,r.styleable.mytitle); Colortext=ta.getcolor (R.styleable.mytitle_textcolor,color.black); Textleft=ta.getstring (R.styleable.mytitle_lefttext); Texttitle=ta.getstring (R.styleable.mytitle_titletext); Textright=ta.getstring (R.styleable.mytitle_righttext); Ta.recycle ();//Get to control //Load layout file, same as Setcontentview () effectLayoutinflater.from (context). Inflate (R.layout.my_view, This); tvleft= (TextView) Findviewbyid (r.id.tv_left); Tvtitle= (TextView) Findviewbyid (r.id.tv_title); tvright= (TextView) Findviewbyid (r.id.tv_right);//Associating a control with a set XML propertyTvleft.settextcolor (Colortext); Tvleft.settext (Textleft); Tvtitle.settextcolor (Colortext); Tvtitle.settext (Texttitle); Tvright.settextcolor (Colortext); Tvright.settext (Textright); }}
My_view.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:layout_width = "match_parent" android:layout_height =" wrap_content " android:orientation =" horizontal " android:padding = "10DP" tools:background< /span>= "@android: Color/holo_blue_dark" ; <textview android:id = "@+id/tv_left" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:textsize =" 18sp " tools:text =" left " tools:textcolor = "#fff" /> <textview android:id = "@+id/tv_title" android:layout_width = "0DP" android:layout_height = "wrap_content" android:layout_weight =" 1 " android:gravity =" center "
android:textsize = "23sp" tools:text = "title" tools : TextColor = "#fff" /> <TextViewandroid:id= "@+id/tv_right"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:textsize=" 18sp " Tools:text="Right"tools:textcolor="#fff"/> </linearlayout>
3. Obtaining a custom attribute name from a third-party namespace
It is important to note that in Activity_main.xml you want to declare a third-party namespace (you only need to use Res-auto in Android Studio, you need to add a full package name in Eclipse, such as)
Note: My_view is just a name to use, the rear "http://schemas.android.com/apk/res-auto" is really useful.
Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"?><relativelayoutxmlns:android="Http://schemas.android.com/apk/res/android" Xmlns:my_view= "http://schemas.android.com/apk/res-auto"android:layout_width=" Match_parent "android:layout_height=" Match_parent " > <com.example.double2.viewxmltest.MyViewandroid:layout_width="Match_parent" android:layout_height="Wrap_content"android:background="@android: Color/holo_ Blue_dark "my_view:lefttext=" Back "my_view:righttext=" Go "my_ View:textcolor="#fff"my_view:titletext="Myviewtest" /> </relativelayout>
Finally attached Source: http://download.csdn.net/detail/double2hao/9594621
Add an XML attribute to a custom view