本文執行個體分析了Android重寫View並自訂屬性的方法。分享給大家供大家參考,具體如下:
這裡通過自訂屬性 實現如下圖所示效果:
第一步:在res\values的目錄下建立一個檔案attrs.xml
聲明一些自訂屬性
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="CustomViewStyle"> <attr name="customText" format="string" /> <attr name="customTextColor" format="color" /> <attr name="customTextSize" format="dimension" /> </declare-styleable></resources>
第二步:在layout目錄下建立布局檔案activity_main.xml
特別注意要在外層控制項加上這個聲明:
格式:xmlns:(你自訂名稱)="http://schemas.android.com/apk/(你應用的包名)"
xmlns:xr="http://schemas.android.com/apk/res/com.rong.test"
或者
xmlns:xr="http://schemas.android.com/apk/res-auto"
推薦使用第二種
在布局檔案中加入這些自訂的屬性:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:xr="http://schemas.android.com/apk/res/com.rong.test" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:orientation="vertical" > <com.rong.activity.CustomView android:layout_width="300dp" android:layout_height="300dp" android:layout_centerInParent="true" android:background="#ff0000" xr:customText="自訂控制項" xr:customTextColor="#000000" xr:customTextSize="40sp" /></RelativeLayout>
第三部繼承View重寫
package com.rong.activity;import com.rong.test.R;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.util.AttributeSet;import android.view.View;/** * 自訂控制項 * * @author 徐榮 * */public class CustomView extends View { /** * 自訂畫筆 */ private Paint mPaint; /** * 文字範圍 */ private Rect mBounds; /** * 自訂文字 */ private String customText; /** * 自訂大小 */ private int customTextSize; /** * 自訂色彩 */ private int customTextColor; public CustomView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomViewStyle); // 擷取自訂文字 customText = typedArray.getString(R.styleable.CustomViewStyle_customText); // 擷取自訂文字大小 customTextSize = typedArray.getDimensionPixelSize(R.styleable.CustomViewStyle_customTextSize, 28); // 或者自訂文字顏色 customTextColor = typedArray.getColor(R.styleable.CustomViewStyle_customTextColor, Color.WHITE); // 要回收這個typedArray對象 typedArray.recycle(); initView(); } public void initView() { // 初始化畫筆 mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(customTextColor); mPaint.setTextSize(customTextSize); // 產生文字地區 mBounds = new Rect(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 擷取文字顯示地區mBounds mPaint.getTextBounds(customText, 0, customText.length(), mBounds); //使文字寬置中顯示=控制項的寬度/2 -文字的寬度/2 float helfWidth = getWidth() / 2 - mBounds.width() / 2; //使文字高置中顯示=控制項的寬度/2 +文字的寬度/2 float helfHeight = getHeight() / 2+mBounds.height()/2; //繪製文字 canvas.drawText(customText, helfWidth, helfHeight, mPaint); }}
運行!
更多關於Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控制項用法總結》
希望本文所述對大家Android程式設計有所協助。