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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

谈谈- declare-styleable属性

發布時間:2023/12/9 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 谈谈- declare-styleable属性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在Android開發中,往往要用到自定義的控件來實現我們的需求或效果。在使用自定義?
控件時,難免要用到自定義屬性,那怎么使用自定義屬性呢?

一、簡單使用:

?

1.在文件res/values/下新建attrs.xml屬性文件,中定義我們所需要的屬性: <resources><!-- resource是跟標簽,可以在里面定義若干個declare-styleable --> <declare-styleable name="custom_view"><!-- name定義了變量的名稱 --><attr name="custom_color" format="color"></attr> <!-- 定義對應的屬性,name定義了屬性的名稱 --><attr name="custom_size" format="dimension"></attr> <!--每一個發生要定義format指定其類型,類型包括 reference 表示引用,參考某一資源IDstring 表示字符串color 表示顏色值dimension 表示尺寸值boolean 表示布爾值integer 表示整型值float 表示浮點值fraction 表示百分數enum 表示枚舉值flag 表示位運算--> </declare-styleable> ?

2.在布局中使用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:ldm="http://schemas.android.com/apk/res/com.ldm.learn"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#f6f6f6"android:orientation="vertical"android:padding="10dp" ><com.ldm.learn.CustomTextViewandroid:layout_width="100dp"android:layout_height="100dp"android:text="自定義TextView"ldm:custom_color="#333333"ldm:custom_size="35sp" /></LinearLayout>
2.在代碼中引用: public class CustomTextView extends TextView { private int textSize;//自定義文件大小

? ? ?private int textColor;//自定義文字顏色

? ? //自定義屬性,會調用帶兩個參數的構造方法
? ? public CustomTextView(Context context, AttributeSet attrs) {
? ? ? ? ? ?super(context, attrs);
? ? ? ? ? ?TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.custom_view);//TypedArray屬性對象
? ? ? ? ? ? textSize = ta.getDimensionPixelSize(R.styleable.custom_view_custom_size, 20);//獲取屬性對象中對應的屬性值
? ? ? ? ? ?textColor = ta.getColor(R.styleable.custom_view_custom_color, 0x0000ff);
? ? ? ? ? ?setColorAndSize(textColor, textSize);//設置屬性
? ? ? ? ? ?ta.recycle();
}

? ?public CustomTextView(Context context){

? ? ? ? ? ?super(context);

}

? ?private void setColorAndSize(int textColor, int textSize){

? ? ? ? ? ?setTextColor(textColor);

? ? ? ? ? setTextSize(textSize);

}

?

}

?

?

?

二、declare-styleable屬性詳解:

?

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

??? (1)屬性定義:

??????????? <declare-styleable name = "名稱">

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

??????????? </declare-styleable>

??? (2)屬性使用:

???????????? <ImageView

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

???????????????????? />

2. color:顏色值。

??? (1)屬性定義:

??????????? <declare-styleable name = "名稱">

?????????????????? <attr name = "textColor" format = "color" />

??????????? </declare-styleable>

??? (2)屬性使用:

??????????? <TextView

???????????????????? android:layout_width = "42dip"
???????????????????? android:layout_height = "42dip"
???????????????????? android:textColor = "#00FF00"

???????????????????? />

3. boolean:布爾值。

??? (1)屬性定義:

??????????? <declare-styleable name = "名稱">

?????????????????? <attr name = "focusable" format = "boolean" />

??????????? </declare-styleable>

??? (2)屬性使用:

??????????? <Button

??????????????????? android:layout_width = "42dip"
??????????????????? android:layout_height = "42dip"

??????????????????? android:focusable = "true"

??????????????????? />

4. dimension:尺寸值。

??? (1)屬性定義:

??????????? <declare-styleable name = "名稱">

?????????????????? <attr name = "layout_width" format = "dimension" />

??????????? </declare-styleable>

??? (2)屬性使用:

??????????? <Button

??????????????????? android:layout_width = "42dip"
??????????????????? android:layout_height = "42dip"

??????????????????? />

5. float:浮點值。

??? (1)屬性定義:

??????????? <declare-styleable name = "AlphaAnimation">

?????????????????? <attr name = "fromAlpha" format = "float" />
?????????????????? <attr name = "toAlpha" format = "float" />

??????????? </declare-styleable>

??? (2)屬性使用:

??????????? <alpha
?????????????????? android:fromAlpha = "1.0"
?????????????????? android:toAlpha = "0.7"

?????????????????? />

6. integer:整型值。

??? (1)屬性定義:

??????????? <declare-styleable name = "AnimatedRotateDrawable">

?????????????????? <attr name = "visible" />
?????????????????? <attr name = "frameDuration" format="integer" />
?????????????????? <attr name = "framesCount" format="integer" />
?????????????????? <attr name = "pivotX" />
?????????????????? <attr name = "pivotY" />
?????????????????? <attr name = "drawable" />

??????????? </declare-styleable>

??? (2)屬性使用:

??????????? <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)屬性定義:

??????????? <declare-styleable name = "MapView">
?????????????????? <attr name = "apiKey" format = "string" />
??????????? </declare-styleable>

??? (2)屬性使用:

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

??????????????????? />

8. fraction:百分數。

??? (1)屬性定義:

??????????? <declare-styleable name="RotateDrawable">
?????????????????? <attr name = "visible" />
?????????????????? <attr name = "fromDegrees" format = "float" />
?????????????????? <attr name = "toDegrees" format = "float" />
?????????????????? <attr name = "pivotX" format = "fraction" />
?????????????????? <attr name = "pivotY" format = "fraction" />
?????????????????? <attr name = "drawable" />
??????????? </declare-styleable>

??? (2)屬性使用:

??????????? <rotate

?????????????????? xmlns:android = "http://schemas.android.com/apk/res/android"
  ???????????? android:interpolator = "@anim/動畫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)屬性定義:

??????????? <declare-styleable name="名稱">
?????????????????? <attr name="orientation">
????????????????????????? <enum name="horizontal" value="0" />
????????????????????????? <enum name="vertical" value="1" />
?????????????????? </attr>????????? ?

??????????? </declare-styleable>

??? (2)屬性使用:

??????????? <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:位或運算。

???? (1)屬性定義:

???????????? <declare-styleable name="名稱">
??????????????????? <attr name="windowSoftInputMode">
??????????????????????????? <flag name = "stateUnspecified" value = "0" />
??????????????????????????? <flag name = "stateUnchanged" value = "1" />
??????????????????????????? <flag name = "stateHidden" value = "2" />
??????????????????????????? <flag name = "stateAlwaysHidden" value = "3" />
??????????????????????????? <flag name = "stateVisible" value = "4" />
??????????????????????????? <flag name = "stateAlwaysVisible" value = "5" />
??????????????????????????? <flag name = "adjustUnspecified" value = "0x00" />
??????????????????????????? <flag name = "adjustResize" value = "0x10" />
??????????????????????????? <flag name = "adjustPan" value = "0x20" />
??????????????????????????? <flag name = "adjustNothing" value = "0x30" />
???????????????????? </attr>?????? ?

???????????? </declare-styleable>

???? (2)屬性使用:

??????????? <activity

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

特別要注意:

???? 屬性定義時可以指定多種類型值。

??? (1)屬性定義:

??????????? <declare-styleable name = "名稱">

?????????????????? <attr name = "background" format = "reference|color" />

??????????? </declare-styleable>

??? (2)屬性使用:

???????????? <ImageView

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

???????????????????? />

轉載:http://blog.csdn.net/langxinlen/article/details/50343175

轉載于:https://www.cnblogs.com/zly1022/p/7526198.html

總結

以上是生活随笔為你收集整理的谈谈- declare-styleable属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧洲精品无码一区二区 | youjizz.com最新| 国产精品高清在线 | 性欧美1819性猛交 | 亚洲综合激情另类小说区 | 夜夜天天干 | 亚洲综合色网 | 国产成人一区二区三区免费看 | 国产手机在线 | 91婷婷 | 五月天激情啪啪 | 18做爰免费视频网站 | 日韩性大片 | 青青草视频成人 | 天天夜碰日日摸日日澡性色av | 东方影库av| 丰满少妇xoxoxo视频 | 午夜影院入口 | 影音先锋人妻啪啪av资源网站 | 中字幕一区二区三区乱码 | 成在线人免费视频 | 黄色网www | 校园激情亚洲 | 欧美精品乱码99久久蜜桃 | 经典三级av在线 | 国产成人无码久久久精品天美传媒 | 韩国19主播内部福利vip | 高h乱l高辣h文短篇h | 最近最好的2019中文 | 国模视频在线 | 欧美天天色| 中文字幕在线1 | 黄色一级大片 | 国产精品视频一区在线观看 | 西西人体大胆4444ww张筱雨 | 2024国产精品视频 | 欧美国产视频一区 | 91av不卡 | av资源在线 | 9191久久| 国产情侣一区二区三区 | 天天看片天天操 | 午夜色影院 | 久久国产精品免费看 | 国产美女主播在线 | 高清在线一区二区 | 一道本av在线 | 欧美成人综合在线 | caoporn视频在线 | 国产乱码一区二区三区在线观看 | 在线视频免费观看一区 | 国产精品久久亚洲 | 伊人成年网 | 亚洲精品观看 | 亚洲宗人网 | 手机在线观看av网站 | 亚洲天堂av中文字幕 | 97超碰色| av中文在线资源 | 青青草色视频 | 久久久久久久久网站 | 国产亚洲天堂网 | 黄页网站免费观看 | 夜夜撸影院| 久久不射电影网 | 中文字幕校园春色 | 伊人影院亚洲 | 日本少妇性生活 | 6699嫩草久久久精品影院 | 制服丝袜在线一区 | 国产中文字幕在线免费观看 | 国语对白精彩对话 | 国产网站av | 黄色一集片| 成人欧美一区二区三区 | 羞羞涩涩网站 | 国产伦精品一区二区三区免费迷 | 一级全黄少妇性色生活片 | 欧美日韩综合一区 | 91视频免费在观看 | 免费在线网站 | 亚洲国产理论 | 亚洲精品视频在线播放 | 亚洲AV无码成人精品国产一区 | 深夜福利视频在线观看 | 毛片视频大全 | 久久亚洲AV成人无码一二三 | 少妇无码一区二区三区免费 | 精品一区在线视频 | 美女极度色诱图片www视频 | 亚洲一二三四视频 | 爽妇网av | www.男女| 黄色片大全| av五十路 | 蜜桃视频中文字幕 | 草逼免费视频 | 免费在线观看黄色av | 午夜理伦三级理论 |