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

第一步:在res\values的目錄下新建一個(gè)文件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
特別注意要在外層控件加上這個(gè)聲明:
格式: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);
// 要回收這個(gè)typedArray對(duì)象
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開(kāi)發(fā)入門與進(jìn)階教程》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- 自己實(shí)現(xiàn)的android樹控件treeview
- Android之IphoneTreeView帶組指示器的ExpandableListView效果
- Android實(shí)現(xiàn)樹形層級(jí)ListView
- Android提高之多級(jí)樹形菜單的實(shí)現(xiàn)方法
- Android設(shè)置TextView顯示指定個(gè)數(shù)字符,超過(guò)部分顯示...(省略號(hào))的方法
- Android重寫TextView實(shí)現(xiàn)文字整齊排版的方法(附demo源碼下載)
- Android實(shí)現(xiàn)ImageView圖片雙擊放大及縮小
- Android獲取屏幕或View寬度和高度的方法
- Android中使用TextView實(shí)現(xiàn)圖文混排的方法
- Android手勢(shì)滑動(dòng)實(shí)現(xiàn)ImageView縮放圖片大小
- Android TreeView效果實(shí)現(xiàn)方法(附demo源碼下載)
相關(guān)文章
Android 頁(yè)面多狀態(tài)布局管理的開(kāi)發(fā)
頁(yè)面多狀態(tài)布局是開(kāi)發(fā)中常見(jiàn)的需求,即頁(yè)面在不同狀態(tài)需要顯示不同的布局,這篇文章主要介紹了Android 頁(yè)面多狀態(tài)布局管理的開(kāi)發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Android退出應(yīng)用最優(yōu)雅的方式(改進(jìn)版)
這篇文章主要介紹了Android退出應(yīng)用最優(yōu)雅的方式,改進(jìn)版,感興趣的小伙伴們可以參考一下2016-01-01
Android EditText實(shí)現(xiàn)扁平化的登錄界面
這篇文章主要為大家詳細(xì)介紹了Android EditText實(shí)現(xiàn)扁平化的登錄界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android自定義扇形倒計(jì)時(shí)實(shí)例代碼
最近工作中需要做一個(gè)倒計(jì)時(shí),是那種一個(gè)圓,慢慢的被吃掉的動(dòng)畫倒計(jì)時(shí),由于自己是android小白,效果還不是多滿意,先給大家分享實(shí)例代碼,僅供大家參考2017-03-03
Android編程實(shí)現(xiàn)TextView部分顏色變動(dòng)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)TextView部分顏色變動(dòng)的方法,涉及Android針對(duì)TextView樣式操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android自定義view實(shí)現(xiàn)雪花特效實(shí)例代碼
實(shí)現(xiàn)雪花的效果其實(shí)也可以通過(guò)自定義View的方式來(lái)實(shí)現(xiàn)的,而且操作上也相對(duì)簡(jiǎn)單一些,下面這篇文章主要給大家介紹了關(guān)于Android自定義view實(shí)現(xiàn)雪花特效的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼
這篇文章主要介紹了Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼,是Android程序開(kāi)發(fā)中非常重要的技巧,需要的朋友可以參考下2014-09-09
Android實(shí)現(xiàn)給TableLayou繪制邊框的方法
這篇文章主要介紹了Android實(shí)現(xiàn)給TableLayou繪制邊框的方法,涉及Android TableLayou布局控制相關(guān)技巧,需要的朋友可以參考下2016-03-03

