自定義View簡(jiǎn)介
自定義View簡(jiǎn)介
簡(jiǎn)單理解,就是在系統(tǒng)自帶的空間滿足不了你的需求的時(shí)候妆档,你就會(huì)使用自定義View观腊。
- 繼承View
public class TextView extends View
- 創(chuàng)建三個(gè)構(gòu)造方法名船,這三個(gè)構(gòu)造方法會(huì)在不同的情況下被調(diào)用
//在new的時(shí)候會(huì)被調(diào)用
public TextView(Context context) {
this(context,null);
}
//在布局layout中使用
public TextView(Context context, @Nullable AttributeSet attrs) {
this(context,null,0);
}
//在布局layout中使用诈茧,但是需要有style
public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
- onMeasure方法,該方法主要用來自定義View的測(cè)量
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//widthMeasureSpec:包含兩個(gè)信息纺念,共32位贝椿,mode(前2位),size(后30位)
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//布局的寬高都是用這個(gè)方法來制定
//指定控件的寬高陷谱,需要測(cè)量
//獲取寬高的模式
int widthMode = MeasureSpec.getMode(widthMeasureSpec); //獲取前2位
int widthSize = MeasureSpec.getSize(widthMeasureSpec); //獲取后30位
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthMode == MeasureSpec.AT_MOST){
//MeasureSpec.AT_MOST 在布局中指定了warp_content
}else if (widthMode == MeasureSpec.EXACTLY){
//MeasureSpec.EXACTLY 在布局中指定了確定的值(100dp)烙博,match_content或fill_parent
}else if (widthMode == MeasureSpec.UNSPECIFIED){
//盡可能大,很少用到 烟逊,ListView
//ListView測(cè)量的時(shí)候只有一個(gè)item
//ScrollView在測(cè)量布局的時(shí)候會(huì)用UNSPECIFIED
}
}
widthMeasureSpec:包含兩個(gè)信息渣窜,共32位,mode(前2位)宪躯,size(后30位)
可以通過MeasureSpec.getMode(widthMeasureSpec)
來獲取前2位乔宿,MeasureSpec.getSize(widthMeasureSpec)
獲取后30位
mode主要有三種分別是AT_MOST,EXACTLY,UNSPECIFIED。
AT_MOST 在布局中指定了warp_content
EXACTLY 在布局中指定了確定的值100dp
访雪,match_content
或fill_parent
UNSPECIFIED 盡可能大详瑞,很少用到掂林。ScrollView在測(cè)量布局的時(shí)候會(huì)用UNSPECIFIED“酉穑可以自行百度ScrollView
+ListView
顯示不全問題泻帮。
- onDraw方法,主要用來繪制
- onTouchEvent 處理跟用戶交互计寇,手指觸摸
說到自定義View里面就有會(huì)自定義屬性锣杂,下面來說下自定義屬性如何創(chuàng)建和獲取。
- 首先在
res
下的value
文件夾中新建attrs.xml
文件番宁,當(dāng)然你可以叫其他的名字元莫。(一般都這么叫)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextView">
<!--
name 屬性名稱
format 格式:
string 文字
color 顏色
dimension 寬高 字體大小
integer 數(shù)字
reference資源(drawable)
-->
<attr name="text" format="string"/>
<attr name="textColor" format="color"/>
<attr name="textSize" format="dimension"/>
<attr name="maxLength" format="integer"/>
<attr name="background" format="reference|color"/>
<!-- 枚舉 -->
<attr name="inputType" >
<enum name="number" value="1"/>
<enum name="text" value="2"/>
</attr>
</declare-styleable>
</resources>
declare-styleable
標(biāo)簽中的name
就是自定義View的類名。
attr
標(biāo)簽中的name
代表屬性名稱贝淤。
format
代表格式柒竞,又分以下幾種政供。string
:文字 播聪、color
:顏色、dimension
:寬高 字體大小布隔、integer
:數(shù)字离陶、reference
:資源(drawable)
- 獲取自定義屬性,在需要有
style
的構(gòu)造方法里衅檀。
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.TextView);
mText = typedArray.getString(R.styleable.TextView_text);
mTextSize = typedArray.getDimensionPixelSize(R.styleable.TextView_textSize,mTextSize);
//還有很多
//回收
typedArray.recycle();
??切記一定要回收招刨!typedArray.recycle()
- 在布局文件中可以通過
app:textSize="12dp"
來使用。
<com.example.view.TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:textSize="12dp"
app:background="@color/colorPrimary"
/>
別忘了導(dǎo)入xmlns:app="http://schemas.android.com/apk/res-auto"