在先前的文章自定義控件——初識自定義控件中,我們已經對自定義控件的進行了描述和分類,其分類分別是
- 自制控件
- 組合控件
- 拓展控件
而且也對幾類控件編寫了一些博文
自制控件1 開關按鈕
自制控件2 —— 自制控件 仿qq側滑菜單
組合控件1—— 設置框
組合控件2——海賊王選項菜單
本文將對 拓展控件 進行一個簡單demo編寫。
何謂拓展控件?
拓展控件 : 繼承自系統已提供的控件并且加上新的功能或者特性。
我們先來做一個最簡單的例子,拓展TextView,讓新建的TextView都有一個藍色邊框,黃色底色。
效果.png
一、拓展TextView
其實要做的很簡單,新建一個類繼承自TextView,然后復寫onDraw方法。
在onDraw里面畫上去兩個矩形,藍色的占據整個View,黃色的小一些。
代碼頁非常簡單
ExpandTextView
public class ExpandTextView extends TextView{
private Paint mPaint;
private Paint inPaint;
public ExpandTextView(Context context) {
super(context);
mPaint = new Paint();
inPaint = new Paint();
}
public ExpandTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
inPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
mPaint.setColor(Color.BLUE);
mPaint.setStrokeWidth(20); // 筆劃大小
mPaint.setStyle(Paint.Style.FILL);
inPaint.setColor(Color.YELLOW);
inPaint.setStrokeWidth(20); // 筆劃大小
inPaint.setStyle(Paint.Style.FILL);
// 繪制一個藍色的矩形
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
// 繪制一個黃色的矩形,比藍色的小一點
canvas.drawRect(8, 8, getMeasuredWidth()-8, getMeasuredHeight()-8, inPaint);
// 這個super在這里必須調用,才能成功繪制文字
super.onDraw(canvas);
}
}
.
.
實用這個拓展控件
activity_main
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.amqr.extendsview.MainActivity">
<com.amqr.extendsview.ExpandTextView
android:id="@+id/mEtv"
android:text="123123"
android:layout_width="100dp"
android:layout_height="60dp"
android:gravity="center"
android:layout_centerInParent="true"
>
</com.amqr.extendsview.ExpandTextView>
</RelativeLayout>
.
.
MainActivity
public class MainActivity extends Activity {
private ExpandTextView mEtv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mEtv = (ExpandTextView) findViewById(R.id.mEtv);
mEtv.setText("Hello");
}
}
運行效果圖:
運行效果圖.png
就這樣,一個最簡單的拓展控件就做好了。
肯定有人說,我還不如直接在TextView直接弄一張背景圖,靈活又直接。沒錯,的確是這樣。
但是上面這個非常簡答的小demo只是為了演示拓展自原生的控件是如何拓展的,通過這個大可以舉一反三。
比如拓展ListView,讓ListView具備左側彈出刪除選項的。郭大嬸有例子
再比如,我們知道ScrollView會和ListView產生沖突,我們常常會在ListView里面重寫onMeasure,改變原生自帶的計算方法,避免兩者的沖突,這樣的其實也算拓展控件。
為了避免沖突我們常常會寫上這么一個拓展ListView
/**
* 重寫ListView,避免在SrollView里面的ListView無法全部展示數據,(避免只能展示一行,必須靠滾動條來滾動查看所有數據)
*
*
*/
public class ListViewForScrollView extends ListView {
public ListViewForScrollView(Context context) {
super(context);
}
public ListViewForScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ListViewForScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
/**
* 重寫該方法,達到使ListView適應ScrollView的效果
*
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
拓展控件的簡單demo就到這里,以后有時間會補上其他比較更好一些的demo。
本篇完。