欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android之AttributeSet案例詳解

 更新時間:2021年08月27日 14:38:21   作者:暗殤  
這篇文章主要介紹了Android之AttributeSet案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
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é)點取出節(jié)點相對應(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案例詳解的文章就介紹到這了,更多相關(guān)Android之AttributeSet內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android編程之軟件的安裝和卸載方法

    Android編程之軟件的安裝和卸載方法

    這篇文章主要介紹了Android編程之軟件的安裝和卸載方法,涉及Android編程實現(xiàn)軟件的安裝、權(quán)限修改及卸載的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-12-12
  • Android中封裝SDK時常用的注解總結(jié)

    Android中封裝SDK時常用的注解總結(jié)

    這篇文章主要給大家總結(jié)了在Android中封裝SDK時常用的注解的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • Android開發(fā)中下拉刷新如何實現(xiàn)

    Android開發(fā)中下拉刷新如何實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Android開發(fā)中下拉刷新的實現(xiàn)方法,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android編程自定義AlertDialog樣式的方法詳解

    Android編程自定義AlertDialog樣式的方法詳解

    這篇文章主要介紹了Android編程自定義AlertDialog樣式的方法,結(jié)合實例形式詳細(xì)分析了Android自定義AlertDialog樣式的具體布局與功能實現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下
    2018-02-02
  • Android使用DatePickerDialog顯示時間

    Android使用DatePickerDialog顯示時間

    本文將結(jié)合實例代碼,介紹Android使用DatePickerDialog顯示時間,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • android studio 3.6 中配置svn的教程

    android studio 3.6 中配置svn的教程

    這篇文章主要介紹了android studio 3.6 配置svn的教程,本文所用的as版本是3.6.1,通過圖文實例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • android開發(fā)教程之間隔執(zhí)行程序(android計時器)

    android開發(fā)教程之間隔執(zhí)行程序(android計時器)

    android開發(fā)中有些情況需要隔一段時間去執(zhí)行某個操作一次或者是每隔一段時間久執(zhí)行某個操作,下面是實現(xiàn)方法
    2014-02-02
  • 詳解Flutter中key的正確使用方式

    詳解Flutter中key的正確使用方式

    這篇文章主要為大家介紹了詳解Flutter中key的正確使用方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • 淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別

    淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別

    這篇文章主要介紹了淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別 ,getMeasuredWidth()獲取的是view原始的大小,getWidth()獲取的是這個view最終顯示的大小,具體區(qū)別介紹大家參考下本文
    2018-04-04
  • android使用handler ui線程和子線程通訊更新ui示例

    android使用handler ui線程和子線程通訊更新ui示例

    這篇文章主要介紹了android使用handler ui線程和子線程通訊更新ui的方法,大家參考使用吧
    2014-01-01

最新評論