Android自訂控制項的實現步驟(二)

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   使用   

前天已經寫了一個關於自訂控制項的實現步驟的部落格,這個是附上代碼的詳細版本

 

首先,我們得建立一個attrs.xml的資源檔,在上面添加我們將要自訂控制項的額外屬性,就是自訂控制項的自訂屬性,具體代碼如下:

<resources>    <declare-styleable name="TestView">        <attr name="textColor" format="color"></attr>        <attr name="textSize" format="dimension"></attr>        <attr name="imgBackground" format="integer"></attr>        <attr name="topBorder" format="boolean"></attr>        <attr name="bottomBorder" format="boolean"></attr>        <attr name="leftBorder" format="boolean"></attr>        <attr name="rightBorder" format="boolean"></attr>    </declare-styleable></resources>

 

然後我們在定義一個自己控制項的類,要繼承相應的控制項比如說你想自訂一個TextView控制項,你就得繼承TextView這個類,然後在類中定義與你上面屬性相對應的變數,然後通過TypedArray類獲得相應的屬性的值,然後也就是最主要的一點,我們得在OnDraw函數中使用,具體代碼如下:

public class MyView extends TextView {    private Paint mTextPaint;    private Paint mBorderPaint;    private Context mContext;    private boolean mTopBorder;    private boolean mBottomBorder;    private boolean mLeftBorder;    private boolean mRightBorder;    public MyView(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub        mContext = context;        initMyView();        // 對於我們自訂的類中,我們需要使用一個名為obtainStyledAttributes的方法來擷取我們的定義。        TypedArray params = context.obtainStyledAttributes(attrs,                R.styleable.TestView);        // 得到自訂控制項的屬性值。        int backgroudId = params.getResourceId(                R.styleable.TestView_imgBackground, 0);        if (backgroudId != 0)            setBackgroundResource(backgroudId);        int textColor = params.getColor(R.styleable.TestView_textColor,                0XFFFFFFFF);        setTextColor(textColor);        float textSize = params.getDimension(R.styleable.TestView_textSize, 40);        setTextSize(textSize);                mTopBorder = params.getBoolean(R.styleable.TestView_topBorder, true);        mLeftBorder = params.getBoolean(R.styleable.TestView_leftBorder, true);        mBottomBorder = params.getBoolean(R.styleable.TestView_bottomBorder, true);        mRightBorder = params.getBoolean(R.styleable.TestView_rightBorder, true);            }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        if (mTopBorder == true)            canvas.drawLine(0, 0, this.getWidth() - 1, 0, mBorderPaint);        if (mRightBorder == true)            canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,                    this.getHeight() - 1, mBorderPaint);        if (mBottomBorder == true)            canvas.drawLine(this.getWidth() - 1, this.getHeight() - 1, 0,                    this.getHeight() - 1, mBorderPaint);        if (mLeftBorder == true)            canvas.drawLine(0, this.getHeight() - 1, 0, 0, mBorderPaint);    }    public void initMyView() {        mTextPaint = new Paint();        mTextPaint.setColor(Color.WHITE);        mBorderPaint = new Paint();        mBorderPaint.setColor(android.graphics.Color.BLACK);    }    public void setTextColor(int textColor) {        mTextPaint.setColor(0XFFAABBCC);    }    public void setTextSize(float textSize) {        mTextPaint.setTextSize(textSize);    }    public void setTopBorder(boolean mTopBorder) {        this.mTopBorder = mTopBorder;    }    public void setBottomBorder(boolean mBottomBorder) {        this.mBottomBorder = mBottomBorder;    }    public void setLeftBorder(boolean mLeftBorder) {        this.mLeftBorder = mLeftBorder;    }    public void setRightBorder(boolean mRightBorder) {        this.mRightBorder = mRightBorder;    }    public void setPaddings(float paddingLeft, float paddingTop) {        setPadding((int) paddingLeft, (int) paddingTop, 0, 0);    }}

 

最後一步就是在你的Activity的布局中使用你所定義的控制項,具體的使用方法就是你自訂控制項的類所在的包的包名加上類名(使用的時候,在設定自訂屬性的時候是沒有提示的,得自己記住自己設定的屬性)

 <myControl.MyView                    android:id="@+id/TextView_Tue"                    android:layout_width="@dimen/cell_width"                    android:layout_height="40dp"                    android:layout_weight="1"                    android:background="@drawable/solid"                    android:text="" />

 

註:最後的注意的是在使用自訂控制項的時候,必須在前面加上自己屬性的引用,如下代碼(入第二行所示):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:myView="http://schemas.android.com/apk/res/com.zsxy_schedule"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.