Android之AttributeSet案例詳解
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的源碼 你會(huì)發(fā)現(xiàn)它是一個(gè)接口 是個(gè)什么接口呢?
熟悉XML解析的人知道 在XML解析中是有AttributeSet這個(gè)東西的,XML解析根據(jù)節(jié)點(diǎn)取出節(jié)點(diǎn)相對應(yīng)的數(shù)據(jù)。
Android中,我們寫的布局文件就是XML形式的,所以這就是每次我們自定義View的時(shí)候,構(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的時(shí)候,AttributeSet又是怎么用的呢?
一般情況下,我們是在values下面新建一個(gè)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案例詳解的文章就介紹到這了,更多相關(guān)Android之AttributeSet內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
RecyclerView無限循環(huán)效果實(shí)現(xiàn)及示例解析
這篇文章主要為大家介紹了RecyclerView無限循環(huán)效果實(shí)現(xiàn)及示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
用xutils3.0進(jìn)行下載項(xiàng)目更新
這篇文章主要介紹了用xutils3.0進(jìn)行下載項(xiàng)目更新的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
ionic監(jiān)聽android返回鍵實(shí)現(xiàn)“再按一次退出”功能
本篇文章主要介紹了ionic監(jiān)聽android返回鍵實(shí)現(xiàn)“再按一次退出”功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-02-02
Android?Jetpack組件Navigation導(dǎo)航組件的基本使用
本篇主要簡單介紹了一下?Navigation?是什么?以及使用它的流程是什么,并且結(jié)合實(shí)際案例?操作了一番,Navigation?還有很多其他用法,如條件導(dǎo)航、嵌套圖、過度動(dòng)畫?等等功能?有機(jī)會(huì)再操作,需要的朋友可以參考下2022-06-06
關(guān)于Android中點(diǎn)擊通知欄的通知啟動(dòng)Activity問題解決
這篇文章主要介紹了關(guān)于解決Android中點(diǎn)擊通知欄的通知啟動(dòng)Activity問題的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03
Android掃描二維碼時(shí)出現(xiàn)用戶禁止權(quán)限報(bào)錯(cuò)問題解決辦法
這篇文章主要介紹了Android掃描二維碼時(shí)出現(xiàn)用戶禁止權(quán)限報(bào)錯(cuò)問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android中監(jiān)聽軟鍵盤顯示狀態(tài)實(shí)現(xiàn)代碼
這篇文章主要介紹了Android中監(jiān)聽軟鍵盤顯示狀態(tài)實(shí)現(xiàn)代碼,本文直接給出核心實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04

