日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

android 自定义xml属性

發(fā)布時(shí)間:2023/12/2 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 自定义xml属性 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android 自定義組件

Android 提供了非常精致的和非常強(qiáng)大的組件化模型,能夠更加方便的構(gòu)建UI,這些UI組件都是基于基本的layout類:View 和 ViewGroup。

部分能夠用的widgets包括:Button,TextView,EditText,ListView,CheckBox,RadioButton,Gallery,Spinner,和一些比較特殊用途的widgets(AutoCompleteTextView,?ImageSwitcher, and?TextSwitcher.)

布局組件有LinearLayout,?FrameLayout,?RelativeLayout,absoluteLayout,TabelLayout

如果預(yù)定義的widgets和布局組件都不符合您的需求,那就需要?jiǎng)?chuàng)建屬于自己的view,如果只是需要對(duì)已有的widget和layout進(jìn)行小部分的調(diào)整,那就可以通過重寫部分一些方法來完成開發(fā)。

下面就舉個(gè)例子講解如何創(chuàng)建自定義的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);} ;@Overrideprotected 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" ><TextViewandroid: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.CustomViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"custom:text="custom view"custom:color="#00FF00"custom:size="18"/></RelativeLayout>

5.?運(yùn)行應(yīng)用

結(jié)果:


轉(zhuǎn)載于:https://www.cnblogs.com/java20130722/archive/2013/03/31/3207276.html

總結(jié)

以上是生活随笔為你收集整理的android 自定义xml属性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。