Android重寫View并自定義屬性實例分析
本文實例分析了Android重寫View并自定義屬性的方法。分享給大家供大家參考,具體如下:
這里通過自定義屬性 實現(xiàn)如下圖所示效果:

第一步:在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/(你應(yīng)用的包名)"
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);
// 生成文字區(qū)域
mBounds = new Rect();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 獲取文字顯示區(qū)域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);
}
}
運(yùn)行!
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- 自己實現(xiàn)的android樹控件treeview
- Android之IphoneTreeView帶組指示器的ExpandableListView效果
- Android實現(xiàn)樹形層級ListView
- Android提高之多級樹形菜單的實現(xiàn)方法
- Android設(shè)置TextView顯示指定個數(shù)字符,超過部分顯示...(省略號)的方法
- Android重寫TextView實現(xiàn)文字整齊排版的方法(附demo源碼下載)
- Android實現(xiàn)ImageView圖片雙擊放大及縮小
- Android獲取屏幕或View寬度和高度的方法
- Android中使用TextView實現(xiàn)圖文混排的方法
- Android手勢滑動實現(xiàn)ImageView縮放圖片大小
- Android TreeView效果實現(xiàn)方法(附demo源碼下載)
相關(guān)文章
Android仿微信activity滑動關(guān)閉效果
這篇文章主要為大家詳細(xì)介紹了Android仿微信activity滑動關(guān)閉的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
Android Mms之:聯(lián)系人管理的應(yīng)用分析
本篇文章是對Android中的聯(lián)系人管理進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
調(diào)用startService會拋出IllegalStateException異常解決
這篇文章主要為大家介紹了調(diào)用startService會拋出IllegalStateException異常解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Android中l(wèi)istview和imageview實現(xiàn)條目單選效果
這篇文章主要為大家詳細(xì)介紹了Android中l(wèi)istview和imageview實現(xiàn)條目單選效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Android 適配器模式應(yīng)用及設(shè)計原理
這篇文章主要介紹了Android 適配器模式應(yīng)用及設(shè)計原理的相關(guān)資料,Android開發(fā)者應(yīng)該都知道適配器會用,但是不是多清楚原理,這里幫大家分析下原理,需要的朋友可以參考下2016-10-10
Android實現(xiàn)原生側(cè)滑菜單的超簡單方式
網(wǎng)上關(guān)于Android實現(xiàn)側(cè)滑菜單的文章有很多,可是我們這篇文章是給大家分享一種超簡單的方式,對大家開發(fā)Android具有一定的參考借鑒價值,有需要的朋友們可以一起來看看。2016-09-09

