溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中怎么自定義xml屬性

發(fā)布時間:2021-06-26 15:45:50 來源:億速云 閱讀:341 作者:Leah 欄目:移動開發(fā)

本篇文章給大家分享的是有關(guān)Android中怎么自定義xml屬性,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

1. 首先創(chuàng)建一個新的android application.

2. 創(chuàng)建屬性
在res/values/ 下創(chuàng)建一個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)建一個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>

以上就是Android中怎么自定義xml屬性,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI