android attributeset总结

时间:5年前   阅读:4235

一般是当项目中遇到这样的场景需要自定义控件的AttributeSet属性:一个自定义控件的有些属性内容是随着外部条件而动态改变的,for example:一个自定义的ListView控件,需要在底部添加一个View,而这个View在不同的模块使用中传入的View是不同的,这时候有两种方法,一种方法就是在自定义ListView控件类中提供一个公开的接口给外部调用从而将View动态的传入进去;另外一种方法就是在通过自定义控件属性,直接类似于系统属性如Android:textsize 的用法 app:boottomView; 通过第二种方法自定义控件在XML中使用时和系统控件的属性使用方法一样,很简单、方便,而且动态、灵活、更具模块框架化,其属性内容直接在xml中动态配置,不了解其原理的人也能将该控件整合到自己的项目中快速使用起来。

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的源码 你会发现它是一个接口 是个什么接口呢?

熟悉XML解析的人知道 在XML解析中是有AttributeSet这个东西的,XML解析根据节点取出节点相对应的数据。

Android中,我们写的布局文件就是XML形式的,所以这就是每次我们自定义View的时候,构造方法有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

    }

本站声明:网站内容来源于网络,如有侵权,请联系我们https://www.qiquanji.com,我们将及时处理。

微信扫码关注

更新实时通知

上一篇:秒速5厘米

下一篇:投资者在50etf期权上的损失只是权利金吗?

网友评论

请先 登录 再评论,若不是会员请先 注册