最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android開(kāi)發(fā)教程之自定義屬性用法詳解

 更新時(shí)間:2014年04月13日 11:09:25   作者:  
這篇文章主要介紹了android開(kāi)發(fā)中的自定義屬性用法詳解,需要的朋友可以參考下

最近項(xiàng)目中經(jīng)常需要用到自定義控件,因此自定義屬性也是經(jīng)常要用到的,在此說(shuō)明一下自定義屬性的用法:

自定義屬性都存在于/value/attr.xml文件中,以如下格式存在。

復(fù)制代碼 代碼如下:

<resource>

<declare-styleable name="自定義屬性名稱(chēng)">

<attr name="屬性名稱(chēng)" format="屬性種類(lèi)"/>

......

</declare-styleable>

</resource>

對(duì)于自定義屬性中的format的值及其含義如下:

format屬性值:reference 、color、boolean、dimension、float、integer、string、fraction、enum、flag

1. reference:參考某一資源ID。

(1)屬性定義:

復(fù)制代碼 代碼如下:

<declare-styleable name = "名稱(chēng)">

   <attr name = "background" format = "reference" />

</declare-styleable>

(2)屬性使用:

復(fù)制代碼 代碼如下:

<ImageView

android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/圖片ID"
/>

2.color:顏色值。

(1)屬性定義:

復(fù)制代碼 代碼如下:

<declare-styleablename="名稱(chēng)">

<attrname="textColor"format="color"/>

</declare-styleable>

(2)屬性使用:

復(fù)制代碼 代碼如下:

<TextView
android:layout_width="42dip"
android:layout_height="42dip"
android:textColor="#00FF00"
/>

3.boolean:布爾值。

(1)屬性定義:

復(fù)制代碼 代碼如下:

<declare-styleablename="名稱(chēng)">

<attrname="focusable"format="boolean"/>

</declare-styleable>

(2)屬性使用:

復(fù)制代碼 代碼如下:

<Button
android:layout_width="42dip"
android:layout_height="42dip"
android:focusable="true"
/>

4.dimension:尺寸值。

(1)屬性定義:

復(fù)制代碼 代碼如下:

<declare-styleablename="名稱(chēng)">

<attrname="layout_width"format="dimension"/>

</declare-styleable>

(2)屬性使用:

復(fù)制代碼 代碼如下:

<Button
android:layout_width="42dip"
android:layout_height="42dip"
/>

5.float:浮點(diǎn)值。

(1)屬性定義:

復(fù)制代碼 代碼如下:

<declare-styleablename="AlphaAnimation">
<attrname="fromAlpha"format="float"/>
<attrname="toAlpha"format="float"/>
</declare-styleable>

(2)屬性使用:

復(fù)制代碼 代碼如下:

<alpha
android:fromAlpha="1.0"
android:toAlpha="0.7"
/>

6.integer:整型值。

(1)屬性定義:

復(fù)制代碼 代碼如下:

<declare-styleablename="AnimatedRotateDrawable">
<attrname="visible"/>
<attrname="frameDuration"format="integer"/>
<attrname="framesCount"format="integer"/>
<attrname="pivotX"/>
<attrname="pivotY"/>
<attrname="drawable"/>
</declare-styleable>

(2)屬性使用:

復(fù)制代碼 代碼如下:

<animated-rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/圖片ID"
android:pivotX="50%"
android:pivotY="50%"
android:framesCount="12"
android:frameDuration="100"
/>

7.string:字符串。

(1)屬性定義:

復(fù)制代碼 代碼如下:

<declare-styleablename="MapView">
<attrname="apiKey"format="string"/>
</declare-styleable>

(2)屬性使用:

復(fù)制代碼 代碼如下:

<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
/>

8.fraction:百分?jǐn)?shù)。

(1)屬性定義:

復(fù)制代碼 代碼如下:

<declare-styleablename="RotateDrawable">
<attrname="visible"/>
<attrname="fromDegrees"format="float"/>
<attrname="toDegrees"format="float"/>
<attrname="pivotX"format="fraction"/>
<attrname="pivotY"format="fraction"/>
<attrname="drawable"/>
</declare-styleable>

(2)屬性使用:

復(fù)制代碼 代碼如下:

<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@anim/動(dòng)畫(huà)ID"

android:fromDegrees="0"
android:toDegrees="360"

android:pivotX="200%"

android:pivotY="300%"
android:duration="5000"

android:repeatMode="restart"

android:repeatCount="infinite"

/>

9.enum:枚舉值。

(1)屬性定義:

復(fù)制代碼 代碼如下:

<declare-styleablename="名稱(chēng)">
<attrname="orientation">
<enumname="horizontal"value="0"/>
<enumname="vertical"value="1"/>
</attr>
</declare-styleable>

(2)屬性使用:

復(fù)制代碼 代碼如下:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>

10.flag:位或運(yùn)算。

(1)屬性定義:

復(fù)制代碼 代碼如下:

<declare-styleablename="名稱(chēng)">
<attrname="windowSoftInputMode">
<flagname="stateUnspecified"value="0"/>
<flagname="stateUnchanged"value="1"/>
<flagname="stateHidden"value="2"/>
<flagname="stateAlwaysHidden"value="3"/>
<flagname="stateVisible"value="4"/>
<flagname="stateAlwaysVisible"value="5"/>
<flagname="adjustUnspecified"value="0x00"/>
<flagname="adjustResize"value="0x10"/>
<flagname="adjustPan"value="0x20"/>
<flagname="adjustNothing"value="0x30"/>
</attr>
</declare-styleable>

(2)屬性使用:

復(fù)制代碼 代碼如下:

<activity
android:name=".StyleAndThemeActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateUnspecified|stateUnchanged | stateHidden">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

特別要注意:

屬性定義時(shí)可以指定多種類(lèi)型值。

(1)屬性定義:

復(fù)制代碼 代碼如下:

<declare-styleablename="名稱(chēng)">
<attrname="background"format="reference|color"/>
</declare-styleable>

(2)屬性使用:

復(fù)制代碼 代碼如下:

<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/圖片ID|#00FF00"
/>

下面說(shuō)說(shuō)AttributeSet與TypedArray在自定義控件中的作用:

AttributeSet的作用就是在控件進(jìn)行初始化的時(shí)候,解析布局文件中該控件的屬性(keyeg:background)與該值(valueeg:@drawable/icon)的信息封裝在A(yíng)ttributeSet中,傳遞給該控件(View)的構(gòu)造函數(shù)。對(duì)于非Android自帶的屬性,在View類(lèi)中處理時(shí)是無(wú)法識(shí)別的,因此需要我們自己解析。所以這就要用到另外一個(gè)類(lèi)TypedArray。在A(yíng)ttributeSet中我們有屬性名稱(chēng),有屬性值,但是控件如何知道哪個(gè)屬性代表什么意思呢?這個(gè)工作就由TypedArray來(lái)做了。TypedArray對(duì)象封裝了/values/attrs.xml中的styleable里定義的每個(gè)屬性的類(lèi)型信息,通過(guò)TypedArray我們就可以知道AttributeSet中封裝的值到底是干什么的了,從而可以對(duì)這些數(shù)據(jù)進(jìn)行應(yīng)用。

AttributeSet就相當(dāng)于一盒糖,TypedArray就相當(dāng)于這盒糖上的標(biāo)簽說(shuō)明,告訴用戶(hù)每個(gè)糖的口味等。這盒糖有什么口味是由用戶(hù)自己的styleable文件里面的內(nèi)容來(lái)決定的。

相關(guān)文章

  • Android ProgressDialog使用總結(jié)

    Android ProgressDialog使用總結(jié)

    ProgressDialog 繼承自AlertDialog,AlertDialog繼承自Dialog,實(shí)現(xiàn)DialogInterface接口,本文給大家介紹Android ProgressDialog使用總結(jié)的相關(guān)知識(shí),需要的朋友通過(guò)此文一起學(xué)習(xí)吧
    2016-01-01
  • Android自定義View畫(huà)圓功能

    Android自定義View畫(huà)圓功能

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)畫(huà)圓功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 詳解Android使用@hide的API的方法

    詳解Android使用@hide的API的方法

    這篇文章主要介紹了詳解Android使用@hide的API的方法的相關(guān)資料,希望通過(guò)本文大家能理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • Android基于service實(shí)現(xiàn)音樂(lè)的后臺(tái)播放功能示例

    Android基于service實(shí)現(xiàn)音樂(lè)的后臺(tái)播放功能示例

    這篇文章主要介紹了Android基于service實(shí)現(xiàn)音樂(lè)的后臺(tái)播放功能,結(jié)合實(shí)例形式分析了Android基于Service組件實(shí)現(xiàn)多媒體音頻播放功能的步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2016-10-10
  • Android精靈動(dòng)畫(huà)用法實(shí)例

    Android精靈動(dòng)畫(huà)用法實(shí)例

    這篇文章主要介紹了Android精靈動(dòng)畫(huà)用法,實(shí)例分析了Android動(dòng)畫(huà)的相關(guān)使用技巧,需要的朋友可以參考下
    2015-06-06
  • Android應(yīng)用關(guān)閉的情況以及識(shí)別方法詳解

    Android應(yīng)用關(guān)閉的情況以及識(shí)別方法詳解

    對(duì)于現(xiàn)在的安卓手機(jī)而言,很多功能都是在逐步完善的,這篇文章主要給大家介紹了關(guān)于A(yíng)ndroid應(yīng)用關(guān)閉的情況以及識(shí)別的相關(guān)資料,文章通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Android10填坑適配指南(實(shí)際經(jīng)驗(yàn)代碼)

    Android10填坑適配指南(實(shí)際經(jīng)驗(yàn)代碼)

    這篇文章主要介紹了Android10填坑適配指南(實(shí)際經(jīng)驗(yàn)代碼),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Jetpack?Compose對(duì)比React?Hooks?API相似度

    Jetpack?Compose對(duì)比React?Hooks?API相似度

    這篇文章主要為大家介紹了Jetpack?Compose對(duì)比React?Hooks?API相似度,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Android 使用Intent傳遞數(shù)據(jù)的實(shí)現(xiàn)思路與代碼

    Android 使用Intent傳遞數(shù)據(jù)的實(shí)現(xiàn)思路與代碼

    Intent是Android中一個(gè)非常重要的概念,跟這個(gè)詞的本意(意圖,目的)一樣,這個(gè)類(lèi)在A(yíng)ndroid中的作用就是要調(diào)用某個(gè)組建去做某一件事,接下來(lái)詳細(xì)介紹,感興趣的朋友可以參考下
    2013-01-01
  • Android 中為什么要用Fragment.setArguments(Bundle bundle)來(lái)傳遞參數(shù)

    Android 中為什么要用Fragment.setArguments(Bundle bundle)來(lái)傳遞參數(shù)

    這篇文章主要介紹了Android 中為什么要用Fragment.setArguments(Bundle bundle)來(lái)傳遞參數(shù),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下
    2017-01-01

最新評(píng)論

乌鲁木齐市| 大足县| 泉州市| 石阡县| 合山市| 油尖旺区| 石台县| 莱州市| 重庆市| 噶尔县| 名山县| 织金县| 怀安县| 南皮县| 沂水县| 新民市| 曲阳县| 彝良县| 大关县| 永泰县| 南安市| 吴堡县| 灵山县| 蓝山县| 白朗县| 新竹市| 睢宁县| 灵山县| 新龙县| 梁平县| 志丹县| 当涂县| 南江县| 枞阳县| 潍坊市| 钟祥市| 保康县| 炎陵县| 区。| 呼玛县| 来凤县|