這篇“Android中如何自定義xml屬性”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Android中如何自定義xml屬性”文章吧。

1. 首先創(chuàng)建一個(gè)新的android application.
2. 創(chuàng)建屬性
在res/values/ 下創(chuàng)建一個(gè)attr.xml 文件,定義好需要的attributes
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="custom"> <attr name="text" format="string" /> <attr name="size" format="integer"/> <attr name="color" format="reference|color"/> </declare-styleable> </resources>
3. 創(chuàng)建自定義的View
創(chuàng)建一個(gè)View, CustomView 繼承自View(根據(jù)具體的情況,如果需求和已經(jīng)存在的widget或者layout相差不大,就繼承,重寫一些方法)
package com.hualu.androidview; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class CustomView extends View { private Paint p = null; private String text = null; public CustomView(Context context) { super(context); initCustomView() ; } public CustomView(Context context, AttributeSet attrs){ super(context, attrs ) ; initCustomView() ; TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.custom) ; int indexCount = a.getIndexCount() ; for(int i = 0 ; i < indexCount ; i ++){ int index = a.getIndex(i) ; switch (index) { case R.styleable.custom_text: text = a.getString(index) ; break; case R.styleable.custom_size: p.setTextSize(a.getInt(index, 0)); break; case R.styleable.custom_color: p.setColor(a.getColor(index, 0xFF000000)) ; break; } } a.recycle() ; } void initCustomView(){ p = new Paint(); p.setAntiAlias(true); } ; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawText(text, 10, 10, p) ; } }4. 在layout的文件使用自定義的view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.hualu.androidview" xmlns:tools="http://schemas.android.com/tools" 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.hualu.androidview.CustomView android:layout_width="wrap_content" android:layout_height="wrap_content" custom:text="custom view" custom:color="#00FF00" custom:size="18" /> </RelativeLayout>
以上就是關(guān)于“Android中如何自定義xml屬性”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞名稱:Android中如何自定義xml屬性-創(chuàng)新互聯(lián)
路徑分享:http://chinadenli.net/article26/diisjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站改版、做網(wǎng)站、域名注冊、商城網(wǎng)站、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容