您好,登錄后才能下訂單哦!
這篇文章主要介紹“Android中AttributeSet的實例用法”,在日常操作中,相信很多人在Android中AttributeSet的實例用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Android中AttributeSet的實例用法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
public interface AttributeSet { /** * Returns the number of attributes available in the set. * * @return A positive integer, or 0 if the set is empty. */ public int getAttributeCount(); /** * Returns the name of the specified attribute. * * @param index Index of the desired attribute, 0...count-1. * * @return A String containing the name of the attribute, or null if the * attribute cannot be found. */ public String getAttributeName(int index); /** * Returns the value of the specified attribute as a string representation. * * @param index Index of the desired attribute, 0...count-1. * * @return A String containing the value of the attribute, or null if the * attribute cannot be found. */ public String getAttributeValue(int index); /** * Returns the value of the specified attribute as a string representation. * The lookup is performed using the attribute name. * * @param namespace The namespace of the attribute to get the value from. * @param name The name of the attribute to get the value from. * * @return A String containing the value of the attribute, or null if the * attribute cannot be found. */ public String getAttributeValue(String namespace, String name);
查看AttributeSet的源碼 你會發(fā)現(xiàn)它是一個接口 是個什么接口呢?
熟悉XML解析的人知道 在XML解析中是有AttributeSet這個東西的,XML解析根據(jù)節(jié)點(diǎn)取出節(jié)點(diǎn)相對應(yīng)的數(shù)據(jù)。
Android中,我們寫的布局文件就是XML形式的,所以這就是每次我們自定義View的時候,構(gòu)造方法有AttributeSet的原因。
SDK給出的解釋如下:
A collection of attributes, as found associated with a tag in an XML document. Often you will not want to use this interface directly, instead passing it to Resources.Theme.obtainStyledAttributes() which will take care of parsing the attributes for you. In particular, the Resources API will convert resource references (attribute values such as "@string/my_label" in the original XML) to the desired type for you; if you use AttributeSet directly then you will need to manually check for resource references (with getAttributeResourceValue(int, int)) and do the resource lookup yourself if needed. Direct use of AttributeSet also prevents the application of themes and styles when retrieving attribute values. This interface provides an efficient mechanism for retrieving data from compiled XML files, which can be retrieved for a particular XmlPullParser through Xml.asAttributeSet(). Normally this will return an implementation of the interface that works on top of a generic XmlPullParser, however it is more useful in conjunction with compiled XML resources:
那我們自定義View的時候,AttributeSet又是怎么用的呢?
一般情況下,我們是在values下面新建一個attrs文件夾
<declare-styleable name="MyView"> <attr name="textColor" format="color"/> <attr name="textSize" format="dimension"/> </declare-styleable>
布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/com.example.androidtest" android:layout_width="match_parent" android:orientation="vertical" android:background="@android:color/black" android:layout_height="match_parent"> <com.example.androidtest.MyView android:layout_width="fill_parent" android:layout_height="wrap_content" myapp:textColor="#FFFFFFFF" myapp:textSize="62dp" ></com.example.androidtest.MyView> </LinearLayout>
自定義View樣例代碼:
public class MyView extends TextView { public MyView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView); int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); float textSize = array.getDimension(R.styleable.MyView_textSize, 36); setTextColor(textColor); setTextSize(textSize); setText("22222222222"); array.recycle(); } public MyView(Context context) { super(context); // TODO Auto-generated constructor stub }
到此,關(guān)于“Android中AttributeSet的實例用法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。