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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 自定义属性时TypedArray的使用

發(fā)布時(shí)間:2024/4/15 Android 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 自定义属性时TypedArray的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

對(duì)于自定義屬性,遵循以下幾步,就可以實(shí)現(xiàn):

  • 自定義一個(gè)CustomView(extends View )類
  • 編寫res/values/attrs.xml,在其中編寫styleable和item等標(biāo)簽元素
  • 在布局文件中CustomView使用自定義的屬性(注意namespace)
  • 在CustomView的構(gòu)造方法中通過TypedArray獲取

  • <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/color_white"android:gravity="center_horizontal"android:orientation="vertical"> <com.demo.RoundCornerProgress android:id="@+id/progressBar"android:layout_width="300dp"android:layout_height="13dp"app:rcMax="100000"app:rcBackgroundColor="#dedede"app:rcProgressColor="#fcc329"app:rcRadius="6.5dp" /></LinearLayout>

    com.demo.RoundCornerProgress是自定義的ProgressBar,布局文件中

    app:rcMax="100000" app:rcBackgroundColor="#dedede" app:rcProgressColor="#fcc329" app:rcRadius="6.5dp"

    是自定義的屬性。

    app是命名空間,自己可以隨便命名其他名字,用來加在自定義屬性前面。

    xmlns:android=”http://schemas.android.com/apk/res/android

    聲明xml命名空間。xmlns意思為“xml namespace”.冒號(hào)后面是給這個(gè)引用起的別名。
    schemas是xml文檔的兩種約束文件其中的一種,規(guī)定了xml中有哪些元素(標(biāo)簽)、元素有哪些屬性及各元素的關(guān)系,當(dāng)然從面向?qū)ο蟮慕嵌壤斫鈙chemas文件可以認(rèn)為它是被約束的xml文檔的“類”或稱為“模板”。

    早期或簡單的xml用的是另一種約束,稱為DTD,這東西大家天天都見到。html/xhtml中都存在(早期的html可能沒有),如” html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“。

    現(xiàn)在大部分xml文檔的約束都換成schema了,原因是schema本身也是xml,二schema擴(kuò)展性強(qiáng)。

    rcMax、rcProgress等就是xml里面自己命名的。

    <?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"><!--進(jìn)度條樣式--><declare-styleable name="RoundCornerProgress"><attr name="rcReverse" format="boolean"/><attr name="rcProgress" format="float"/><attr name="rcMax" format="float"/><attr name="rcSecondaryProgress" format="float"/><attr name="rcBackgroundPadding" format="dimension"/><attr name="rcRadius" format="dimension"/><attr name="rcProgressColor" format="color"/><attr name="rcSecondaryProgressColor" format="color"/><attr name="rcBackgroundColor" format="color"/></declare-styleable> </resources>

    其中的format的意義和可取的值有以下一些:

    • reference:表示引用,參考某一資源ID
      (1)屬性定義:
      (2)屬性使用:
    <ImageViewandroid:layout_width = "42dip"android:layout_height = "42dip"android:background = "@drawable/圖片ID"/>
    • color:顏色值
    • boolean:布爾值
    • dimension:尺寸值。注意,這里如果是dp那就會(huì)做像素轉(zhuǎn)換
    • float:浮點(diǎn)值。
    • integer:整型值。
    • string:字符串
    • fraction:百分?jǐn)?shù)。
    • enum:枚舉值
    • flag:是自己定義的,類似于 android:gravity=”top”,就是里面對(duì)應(yīng)了自己的屬性值。
    • reference|color:顏色的資源文件。 12.reference|boolean:布爾值的資源文件

    注意://由于reference是從資源文件中獲取:所以在XML文件中寫這個(gè)屬性的時(shí)候必須personattr:name=”@string/app_name”這種格式,否則會(huì)出錯(cuò)


    接著就可以在自定義控件中獲取了

    context通過調(diào)用obtainStyledAttributes方法來獲取一個(gè)TypeArray,然后由該TypeArray來對(duì)屬性進(jìn)行設(shè)置
    obtainStyledAttributes方法有三個(gè),我們最常用的是有一個(gè)參數(shù)的obtainStyledAttributes(int[] attrs),其參數(shù)直接styleable中獲得
    TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
    調(diào)用結(jié)束后務(wù)必調(diào)用recycle()方法,否則這次的設(shè)定會(huì)對(duì)下次的使用造成影響

    public void setupStyleable(Context context, AttributeSet attrs) {TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerProgress);radius = (int) typedArray.getDimension(R.styleable.RoundCornerProgress_rcRadius, dp2px(DEFAULT_PROGRESS_RADIUS));padding = (int) typedArray.getDimension(R.styleable.RoundCornerProgress_rcBackgroundPadding,dp2px(DEFAULT_BACKGROUND_PADDING));isReverse = typedArray.getBoolean(R.styleable.RoundCornerProgress_rcReverse, false);max = typedArray.getFloat(R.styleable.RoundCornerProgress_rcMax, DEFAULT_MAX_PROGRESS);progress = typedArray.getFloat(R.styleable.RoundCornerProgress_rcProgress, DEFAULT_PROGRESS);secondaryProgress = typedArray.getFloat(R.styleable.RoundCornerProgress_rcSecondaryProgress,DEFAULT_SECONDARY_PROGRESS);int colorBackgroundDefault = context.getResources().getColor(R.color.round_corner_progress_bar_background_default);colorBackground = typedArray.getColor(R.styleable.RoundCornerProgress_rcBackgroundColor, colorBackgroundDefault);int colorProgressDefault = context.getResources().getColor(R.color.round_corner_progress_bar_progress_default);colorProgress = typedArray.getColor(R.styleable.RoundCornerProgress_rcProgressColor, colorProgressDefault);int colorSecondaryProgressDefault = context.getResources().getColor(R.color.round_corner_progress_bar_secondary_progress_default);colorSecondaryProgress = typedArray.getColor(R.styleable.RoundCornerProgress_rcSecondaryProgressColor,colorSecondaryProgressDefault);typedArray.recycle();initStyleable(context, attrs);}

    在控件構(gòu)造方法中調(diào)用該方法就能獲取到值了

    public RoundCornerProgressBar(Context context, AttributeSet attrs) {setup(context, attrs);}

    AttributeSet中的確保存的是該View聲明的所有的屬性,并且可以通過它去獲取(自定義的)屬性。

    public CustomView(Context context, AttributeSet attrs) {super(context, attrs);int count = attrs.getAttributeCount();for (int i = 0; i < count; i++) {String attrName = attrs.getAttributeName(i);String attrVal = attrs.getAttributeValue(i);Log.e(TAG, "attrName = " + attrName + " , attrVal = " + attrVal);}}

    就能打印出所有屬性。

    attrName = layout_width , attrVal = 300.0dip
    attrName = layout_height , attrVal = 13.0dip
    attrName = app:rcMax , attrVal = 100000.0

    總結(jié)

    以上是生活随笔為你收集整理的Android 自定义属性时TypedArray的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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