1. Custom View
The layout file is as follows:
<?xml version="1.0" encoding="utf-8"?><A> <B></B></A>
A extends linerlayout
B extends View
(Ignore layout)
Practice:
1. Create a class myview integrated view class.
public class MyBrick extends View
2. Reload the three constructor methods and ondraw methods in the parent class.
public MyBrick(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public MyBrick(Context context, AttributeSet attrs) {super(context, attrs);}public MyBrick(Context context) {super(context);}
3. Use paint to set the items in the view (for example, we need to display a red rounded rectangle in the view)
@ Overrideprotected void ondraw (canvas) {super. ondraw (canvas); paint = new paint (); paint. setcolor (color. red); // set the color to paint. setstyle (style. fill); // fill the canvas with layout. drawroundrect (New rectf (50, 0,100,100), 15f, 20f, paint); // draw a rounded rectangle}
4. Use the following in the layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.brickbreaker.childview.MyBrickandroid:layout_width="wrap_content"android:layout_height="wrap_content" > </com.example.brickbreaker.childview.MyBrick></RelativeLayout>
Ii. Custom Attributes
1. Create attrs. xml under Res/Values
The content is as follows:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyBrick"><attr name="joeColor" format="color"></attr><attr name="joeTextSize" format="dimension"></attr> </declare-styleable></resources>
The value of name attribute in declare-styleable is the name of the custom view.
The name in the ATTR tag is the name of the custom attribute. Format indicates the type or format of the custom attribute.
For example, joecolor is a color attribute.
Some other common values of format are as follows:
Reference indicates reference, for example, R. Id. XX.
String indicates a string.
Color indicates the color value.
Dimension indicates the size value.
Boolean indicates a Boolean value.
Integer indicates the integer value.
Float indicates the floating point value.
Fraction indicates percentage
Enum indicates the enumerated value.
Flag bitwise operation
2. Use custom attributes in the Custom View class.
In the custom view constructor Class Name (context, attributeset attrs ),
Path and default value
public MyBrick(Context context, AttributeSet attrs) {super(context, attrs);Paint paint = new Paint();TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyBrick);int color = a.getColor(R.styleable.MyBrick_joeColor, Color.GREEN);float dimension = a.getDimension(R.styleable.MyBrick_joeTextSize, 22);paint.setColor(color);paint.setTextSize(dimension);a.recycle();}
R. styleable. mybrick_joecolor consists of three parts:
R. styleable. mybrick is mybrick in declare-styleable in attrs. xml.
Joecolor is a custom attribute, and "_" underscores are used to connect them together. In this way, when we use custom joecolor in the layout file,
The Virtual Machine knows that we want to add attributes for the custom view.
int color = a.getColor(R.styleable.MyBrick_joeColor, Color.GREEN);float dimension = a.getDimension(R.styleable.MyBrick_joeTextSize, 22);paint.setColor(color);paint.setTextSize(dimension);
The above four statements set the default attribute.
3. Use custom attributes in the layout File
Must be added in the outermost Layout
Xmlns: Joe = "http://schemas.android.com/apk/res/com.example.brickbreaker"
Joe is a custom name called "namespace". The preceding code is used as an example. In the layout file, if you want to use custom attributes for a custom view, to change the namespace
Replace android with Joe, as shown below:
<com.example.brickbreaker.childview.MyBrickandroid:layout_width="wrap_content"android:layout_height="wrap_content"joe:joeColor="#A26FA7" > </com.example.brickbreaker.childview.MyBrick>
Com. example. Brickbreaker is the package name of the application where the custom view is located. This is required and cannot be written at will.