When we customize the view, it is usually done in the layout file when we assign some length widths to the view. , such as android:layout_height= "Wrap_content", in addition, we can also define their own properties, so we can use the shape as myapp:mytextsize= "20sp" in the way.
Values/attrs.xml
The first thing to do is create a variable, create a values/attrs.xml file, or any file name, but in the values directory:
<?XML version= "1.0" encoding= "Utf-8"?> <Resources> <declare-styleablename= "MyView"> <attrname= "TextSize"format= "Dimension" /> </declare-styleable> </Resources>
Where resource is tagged, you can define several declare-styleable,<declare-styleable name= "MyView" in the definition of the name of the variable, and then you can customize multiple properties, for <attr name= "TextSize" format= "Dimension"/>, the name of its property is "TextSize", format specifies that the property type is dimension and can only represent the size of the font.
- Format can also specify other types such as:
- Reference represents a reference, referencing a resource ID
- String representation
- Color indicates the value
- Dimension represents dimension values
- Boolean represents a Boolean value
- Integer denotes integer value
- float indicates floating-point value
- Fraction represents a percentage
- Enum represents an enum value
- Flag indicates bitwise operation
Layout
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:myapp= "Http://schemas.android.com/apk/res/com.eyu.attrtextdemo"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " > <Us.yydcdut.MyViewAndroid:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"myapp:textsize= "20SP"Myapp:mycolor= "#324243"/> </LinearLayout>
Can see more xmlns:myapp= "Http://schemas.android.com/apk/res/com.eyu.attrtextdemo" , And in Custom view myapp:textsize= "20sp",myapp:mycolor= "#324243"
Obtainstyledattributes
The context obtains a typearray by calling the Obtainstyledattributes method, which is then set by the Typearray property
Obtainstyledattributes method has three, we most commonly used is a parameter of Obtainstyledattributes (int[] attrs), its parameters are directly styleable obtained
TypedArray a = context.obtainstyledattributes (Attrs,r.styleable.myview);
Make sure to call the Recycle () method at the end of the call, otherwise this setting will affect the next use.
Public classMyViewextendsview{ PublicPaint paint; PublicMyView (Context context, AttributeSet attrs) {Super(context, attrs); Paint=NewPaint (); TypedArray a=context.obtainstyledattributes (Attrs,r.styleable.myview); intTextColor = A.getcolor (R.styleable.myview_mycolor, 003344); floatTextSize = A.getdimension (r.styleable.myview_mytextsize, 33); Paint.settextsize (textSize); Paint.setcolor (TextColor); A.recycle (); } PublicMyView (Context context) {Super(context); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Paint.setstyle (Style.fill); Canvas.drawtext ("Aaaaaaa", 10, 50, paint); } }I'm the dividing line of the king of the Land Tiger.
Android--TypedArray