Android 自訂控制項-TextView

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   使用   

很多時候系統內建的View滿足不了設計的要求,就需要自訂View控制項。自訂View首先要實現一個繼承自View的類。添加類的構造方法,override父類的方法,如onDraw,(onMeasure)等。如果自訂的View有自己的屬性,需要在values下建立attrs.xml檔案,在其中定義屬性,同時代碼也要做修改。

一個簡單的例子:

·建立一個MyView類,繼承自TextView,並添加構造方法:

 

 

 

package com.example.custview;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.widget.TextView;public class MyView extends TextView {    Paint mPaint = null;    public MyView(Context context) {        super(context);        // TODO Auto-generated constructor stub    }    public MyView(Context ctx, AttributeSet attrs) {        super(ctx, attrs);        mPaint = new Paint();        TypedArray array = ctx                .obtainStyledAttributes(attrs, R.styleable.MyView);        int textColor = array                .getColor(R.styleable.MyView_textColor, 0XFF00FF00); // 提供預設值,放置未指定        float textSize = array.getDimension(R.styleable.MyView_textSize, 36);        mPaint.setColor(textColor);        mPaint.setTextSize(textSize);        array.recycle();    }    @Override    public void draw(Canvas canvas) {        // TODO Auto-generated method stub        super.draw(canvas);        if (mPaint == null) {            mPaint = new Paint();        }        mPaint.setColor(Color.RED);        mPaint.setStrokeCap(Paint.Cap.ROUND);        mPaint.setStrokeWidth(3);        canvas.drawRect(0, 0, 20, 20, mPaint);    }}

 

再在主布局中使用:

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:my="http://schemas.android.com/tools/res/com.example.custview"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="@string/hello_world" /><com.example.custview.MyView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    my:textColor="#FFFFFFFF"    my:textSize="33dp" /> </RelativeLayout>

 

 

在圖形布局中可以看到下面的結果:

 

 

即可完成。運行結果

 

 注意,因為在上面我們用到了布局,也就是自訂的控制項是在xml配置的,所以一定要添加構造方法,(如果沒有在布局中進行空間設定,這個構造方法可以不需要):

public MyView(Context context,AttributeSet attrs){       super(context, attrs);      }至少在xml檔案中寫上上面的內容。其中com.example.custview.MyView這句是需要顯示的控制項所代表的類。這個類肯定是繼承自View的自訂類(其實就是,使我們自己寫的,這是廢話了,可以是在工程中直接源碼添加xxxx.java的,也可以是在libs目錄下自己新添加的jar包裡面的。如果是jar包裡面的一個類,則路徑就是jar包裡面,這個類的路徑。完成上面的兩步之後就可以在代碼中執行個體化這個布局檔案了@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //setContentView(new MyView(this));顯示的效果同。 下面介紹如何?自訂View的屬性設定。實現自訂View的屬性設定,需要:·在values目錄下建立attrs.xml檔案,添加屬性內容·在布局檔案中添加新的命名空間xmlns,然後可以使用命名空間給自訂的空間設定屬性·設定完屬性之後,當然還要對其進行處理。在自訂View類中的構造方法中進行處理根據這三步給一個例子進行說明一下首先添加attrs.xml檔案,在定義屬性<?xml version="1.0" encoding="utf-8"?> <resources>     <declare-styleable name="MyView">     <attr name="textColor" format="color"/>     <attr name="textSize" format="dimension"/>     </declare-styleable> </resources>public MyView(Context context,AttributeSet attrs){       super(context, attrs);         mPaint = new Paint();          //TypedArray是一個用來存放由context.obtainStyledAttributes獲得的屬性的數組          //在使用完成後,一定要調用recycle方法          //屬性的名稱是styleable中的名稱+“_”+屬性名稱          //TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);        int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供預設值,放置未指定          float textSize = array.getDimension(R.styleable.MyView_textSize, 36);          mPaint.setColor(textColor);          mPaint.setTextSize(textSize);          array.recycle(); //一定要調用,否則這次的設定會對下次的使用造成影響            } 

 

Android 自訂控制項-TextView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.