当前位置:网站首页>Custom view
Custom view
2022-06-13 06:06:00 【Little xian】
1 Customize View Most basic element
1) Resource file ,attrs.xml, Describe custom control properties .
2) Customize View class , Realization onDraw Other methods .
3) Add custom... To the layout file view,“ To sign up + Class name ” As label .
4) Add a custom namespace to the layout file xmls, form :xmls: Custom naming =http://schemas.android.com/apk/res/ Package name
2 Resource file
Example :
<declare-styleable name="rainbowbar">
<attr name="rainbowbar_hspace" format="dimension"></attr>
<attr name="rainbowbar_vspace" format="dimension"></attr>
<attr name="rainbowbar_color" format="color"></attr>
</declare-styleable>
analysis :
label declare-styleable Declare style name .
Attr Declare the property name , Format .
Format is divided into :
reference: | Refer to a resource ID |
color | Color value |
boolean | Boolean value |
dimension | Size ,dp |
float | Floating point value |
integer | integer |
string | character string |
Fraction | percentage |
Flag | Bitwise OR operation |
Enum | Enumerated values |
Examples of enumeration types :
(1) Attribute definitions :
<declare-styleable name=" name ">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
(2) Attributes use :
<LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
</LinearLayout>
3 Customize View
Basic steps
A. Create a class that inherits from view, Create a constructor , Basically, there are three construction methods . The three parameters are Context context, AttributeSet attrs, int defStyleAttr
Get custom styles in the constructor , The value of the custom property , To design Paint object .
Specific use TypedArray class .
Code example :
TypedArray t = context.obtainStyledAttributes(attrs,
R.styleable.rainbowbar, 0, 0);
barColor = t.getColor(R.styleable.rainbowbar_rainbowbar_color, barColor);
mPaint.setColor(barColor);
B. Realization Ondraw Method , The method parameters are Canvas.
In this method, you can call Canvas mapping , The brush can be customized paint.
There are basically the following drawing methods :
drawBitmap |
clipPath |
clipRect |
drawText |
drawRect …… |
C. Realization onMeasure Method , In this method, the layout... Is passed in xml The length and width set in , After customized processing, it finally calls setMeasuredDimension Method to set the final View Length and width .
One MeasureSpec Encapsulates the layout requirements passed from the parent layout to the child layout , Every MeasureSpec Represents a set of width and height requirements . One MeasureSpec It's made up of size and pattern . It has three modes :
UNSPECIFIED, Not specified | Generally, the parent control is AdapterView, adopt measure The pattern passed in by the method |
EXACTLY, Self limiting | The precise value is set , Length and width =xxxdp |
AT_MOST, at most | When the control's layout_width or layout_height Designated as WRAP_CONTENT when , Control size generally varies with the control's subspace or content , In this case, the size of the control does not exceed the maximum size allowed by the parent control . therefore , At this time mode yes AT_MOST,size The maximum size allowed by the parent control is given . |
A simple example :
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMySize(200, widthMeasureSpec);
int height = getMySize(200, heightMeasureSpec);
setMeasuredDimension(width, height);
}
private int getMySize(int defaultSize, int measureSpec) {
int mySize = defaultSize;
int mode = MeasureSpec.getMode(measureSpec);
int size = MeasureSpec.getSize(measureSpec);
switch (mode) {
case MeasureSpec.UNSPECIFIED: {// If no size is specified , Set it to the default size
mySize = defaultSize;
break;
}
case MeasureSpec.AT_MOST: {// If the measurement mode is the maximum value is size
// We take the maximum size , The maximum value is this View In the father View The maximum that can be displayed in size.
mySize = size;
break;
}
case MeasureSpec.EXACTLY: {// If it's a fixed size , Then don't change
mySize = size;
break;
}
}
return mySize;
}
4 Layout file settings
First, add a namespace to the layout file xmls, form :(xmls: Custom naming =http://schemas.android.com/apk/res/ Package name )
This formal meeting Automatic check Is there a definition under the package attr Property description .
If not in this form , Set any text content , be Does not automatically check .
Customize View Layout of , To add a namespace name before using a custom attribute .
Example : among rainbow Is the name of the namespace .
<com.example.viewlearn.RainbowBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
rainbow:rainbowbar_color="#766698"
rainbow:rainbowbar_hspace="80dp"
rainbow:rainbowbar_vspace="10dp"
/>
边栏推荐
- MySQL stored procedure
- Leetcode Hamming distance simple
- ffmpeg 下载后缀为.m3u8的视频文件
- Leetcode- keyboard line - simple
- Leetcode- maximum average of subarray i- simple
- @The detailed explanation of configurationproperties and the problem that all properties of the entity bean modified by this annotation are null after injection are solved
- Add attributes in storyboard and Xib (fillet, foreground...) Ibinspectable and ibdesignable
- Leetcode- divide candy - simple
- Experience of redis installation under Linux system (an error is reported at the same time. The struct redis server does not have a member named XXXX)
- Tongweb card, tongweb card, tongweb card
猜你喜欢
【DP之01背包】
Working principle of sentinel series (concept)
Why do so many people hate a-spice
AI实现亲人“复活”|老照片修复|老照片上色,免费APP推荐
Software testing - Summary of common interface problems
A glimpse of [wechat applet]
Echart折线图:多条折线图每次仅展示一条
Echart柱状图:堆叠柱状图value格式化显示
Echart折线图:当多条折线存在相同name,图例仍全部显示
Echart折线图:当多条折线图的name一样时也显示不同的颜色
随机推荐
Essays on November 5, 2021
Ffmpeg download suffix is Video files for m3u8
Echart折线图:当多条折线图的name一样时也显示不同的颜色
php 分布式事务 原理详解
零拷贝技术
Leetcode- reverse string - simple
Shardingsphere JDBC exception: no table route info
The Boys x PUBGMOBILE 联动火热来袭!来看最新游戏海报
= = relation between int and integer
Leetcode fizz buzz simple
[turn] explain awk (1)__ Awk Basics_ Options_ Program segment parsing and examples
Alibaba cloud OSS file download cannot be resumed at a breakpoint
Concurrent programming -- countdownlatch combined with thread pool
Security baseline check script - the road to dream
Echart折线图:当多条折线存在相同name,图例仍全部显示
【自动化测试】Cypress手册
推荐扩容工具,彻底解决C盘及其它磁盘空间不够的难题
js-bom
What happens when the MySQL union index ABC encounters a "comparison operator"?
Echart矩形树图:简单实现矩形树图