知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
andriod中自定義屬性的使用
發(fā)表時間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):36
自定義屬性文件values/attrs.xml:
[img]http://common.cnblogs.com/images/copycode.gif
[img]http://common.cnblogs.com/images/copycode.gif
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="RangeSeekBar"> <attr name="orientation" format="string"/> <attr name="limitThumbRange" format="boolean"/> <attr name="scaleMin" format="float"/> <attr name="scaleMax" format="float"/> <attr name="scaleStep" format="float"/> <attr name="thumb" format="reference"/> <attr name="thumbs" format="integer"/> <attr name="thumbWidth" format="dimension"/> <attr name="thumbHeight" format="dimension"/> <attr name="track" format="reference"/> <attr name="range" format="reference"/> </declare-styleable> </resources>
[img]http://common.cnblogs.com/images/copycode.gif
自定義屬性文件中的attr都是一些自定義屬性,可以在layout文件中的控件屬性中應(yīng)用。
屬性的format有以下幾種:
根據(jù)屬性的類型不合引用方法不合,浮點(diǎn)數(shù)用getFloat,reference用getDrawable。每個屬性名稱都邑在R.styleable下生成一個整數(shù)值用來在java代碼中引用:
1. reference:參考某一資本ID。
2. color:色彩值。
3. boolean:布爾值。
4. dimension:尺寸值。
5. float:浮點(diǎn)值。
6. integer:整型值。
7. string:字符串。
8. fraction:百分?jǐn)?shù)。
9. enum:列舉值。
10. flag:位或運(yùn)算。
RangeSeekBar:thumb="@drawable/thumb"
layout文件中引用自定義屬性:
1. 定義一個屬性的引用前綴:xmlns:RangeSeekBar="http://schemas.android.com/apk/res/diy.example.location" ,
diy.example.location為包名。下面在控件中可以用前綴RangeSeekBar下面在控件中可以用前綴來引用屬性。
thumb是reference類型,引用drawable的一個資本thumb,即在drawable目次下有一個名為thumb的資本文件存在。
2. 在控件中引用屬性:
RangeSeekBar:thumbs="2"
thumbs是整數(shù)類型,初始化為2。 下面是layout文件典范:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:RangeSeekBar="http://schemas.android.com/apk/res/diy.example.location" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ViewLocPathActivity" > <diy.example.location.RangeSeekBar android:id="@+id/seekPath" android:layout_width="match_parent" android:layout_height="30dp" android:layout_alignParentBottom="true" android:layout_marginBottom="50dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" RangeSeekBar:thumb="@drawable/thumb" RangeSeekBar:track="@drawable/trackgradient" RangeSeekBar:range="@drawable/rangegradient" RangeSeekBar:thumbs="2" /> </RelativeLayout>
[img]http://common.cnblogs.com/images/copycode.gif
java代碼中對自定義屬性的引用:
scaleRangeMin = a.getFloat(R.styleable.RangeSeekBar_scaleMin, 0);
thumb = a.getDrawable(R.styleable.RangeSeekBar_thumb); 下面是自定義控件中應(yīng)用自定義屬性典范例代碼:
[img]http://common.cnblogs.com/images/copycode.gif
public RangeSeekBar(Context context, AttributeSet attrs) { super(context, attrs); init(context); // Obtain our styled custom attributes from xml TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.RangeSeekBar); String s = a.getString(R.styleable.RangeSeekBar_orientation); if(s != null) orientation = s.toLowerCase().contains("vertical") ? VERTICAL : HORIZONTAL; limitThumbRange = a.getBoolean(R.styleable.RangeSeekBar_limitThumbRange, true); scaleRangeMin = a.getFloat(R.styleable.RangeSeekBar_scaleMin, 0); scaleRangeMax = a.getFloat(R.styleable.RangeSeekBar_scaleMax, 100); scaleStep = Math.abs(a.getFloat(R.styleable.RangeSeekBar_scaleStep, DEFAULT_STEP)); thumb = a.getDrawable(R.styleable.RangeSeekBar_thumb); range = a.getDrawable(R.styleable.RangeSeekBar_range); track = a.getDrawable(R.styleable.RangeSeekBar_track); // Register desired amount of thumbs int noThumbs = a.getInt(R.styleable.RangeSeekBar_thumbs, DEFAULT_THUMBS); thumbWidth = a.getDimension(R.styleable.RangeSeekBar_thumbWidth, 50); thumbHeight = a.getDimension(R.styleable.RangeSeekBar_thumbHeight, 100); for(int i = 0; i < noThumbs; i++) { Thumb th = new Thumb(); thumbs.add(th); } a.recycle(); }
[img]http://common.cnblogs.com/images/copycode.gif
-------------------------------------------------------------
其他出色文┞仿文┞仿
Android KSOAP2調(diào)用.net webservice
jQuery教程(8)-DOM樹操作之應(yīng)用反向插入辦法
android進(jìn)修標(biāo)記(34)應(yīng)用AlertDialog創(chuàng)建簡單對話框
android進(jìn)修標(biāo)記(33)畫廊視圖(Gallery)的功能和用法
android navidgation drawer 在導(dǎo)航抽屜中若何改變List選中項的...
更多關(guān)于android開辟文┞仿
相關(guān)案例查看更多
相關(guān)閱讀
- 云南網(wǎng)站建設(shè)高手
- 云南網(wǎng)站建設(shè)制作
- 汽車報廢軟件
- 云南小程序被騙蔣軍
- 云南網(wǎng)站建設(shè)首頁
- 昆明小程序公司
- 二叉樹
- 云南建設(shè)廳網(wǎng)站首頁
- 云南軟件設(shè)計
- 云南小程序開發(fā)公司哪家好
- 報廢車回收
- 智慧農(nóng)貿(mào)市場
- 昆明小程序開發(fā)
- 前端
- web
- 網(wǎng)站建設(shè)開發(fā)
- 云南省城鄉(xiāng)建設(shè)廳網(wǎng)站
- 云南省建設(shè)廳網(wǎng)站官網(wǎng)
- 云南etc小程序
- 北京小程序開發(fā)
- 云南省建設(shè)廳網(wǎng)站
- 網(wǎng)站建設(shè)公司網(wǎng)站
- 貴州小程序開發(fā)
- 云南網(wǎng)站維護(hù)
- 小程序退款
- php網(wǎng)站
- 網(wǎng)站開發(fā)哪家好
- 迪慶小程序開發(fā)
- 小程序
- 電商網(wǎng)站建設(shè)