如何获得的AttributeSet属性 [英] how to get AttributeSet properties

查看:964
本文介绍了如何获得的AttributeSet属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有延伸的ViewGroup类

 公共类的MapView扩展的ViewGroup
 

据包含在布局 map_controls.xml 像这样

 < com.xxx.map.MapView
    机器人:ID =@ + ID /图
    机器人:背景=@可绘制/地址
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
< /com.xxx.map.MapView>
 

如何重新获取来自AttributeSet中的构造属性?比方说,在背景场的绘制。

 公共图形页面(上下文的背景下,ATTRS的AttributeSet){
}
 

解决方案

在一般情况下,这样做是这样的:

 公共图形页面(上下文的背景下,ATTRS的AttributeSet){
    // ...

    INT [] attrsArray =新INT [] {
        android.R.attr.id,// 0
        android.R.attr.background,// 1
        android.R.attr.layout_width,// 2
        android.R.attr.layout_height // 3
    };
    TypedArray TA = context.obtainStyledAttributes(ATTRS,attrsArray);
    INT ID = ta.getResourceId(0 / *指数attrsArray * /,View.NO_ID属性);
    可绘制背景= ta.getDrawable(1);
    INT layout_width = ta.getDimensionPixelSize(2,ViewGroup.LayoutParams.MATCH_PARENT);
    INT layout_height = ta.getDimensionPixelSize(3 ViewGroup.LayoutParams.MATCH_PARENT);
    ta.recycle();
}
 

注意怎样的元素中的 attrsArray 的事情的索引。然而,在特定的情况下,它的工作原理一样好,用干将,就像你发现自己的:

 公共图形页面(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS); //在此之后,使用普通的getter

    INT的id = this.getId();
    可绘制背景= this.getBackground();
    ViewGroup.LayoutParams的LayoutParams = this.getLayoutParams();
}
 

这工作,因为你有 com.xxx.map.MapView 属性  是的查看基类构造函数分析的基本属性。如果你想定义的的属性,看看这个问题,并以优良的答案:<一href="http://stackoverflow.com/questions/2695646/declaring-a-custom-android-ui-element-using-xml">Declaring一个定制的Andr​​oid UI元素使用XML

Suppose i have a class which extends ViewGroup

public class MapView extends ViewGroup

It is included in the layout map_controls.xml like this

<com.xxx.map.MapView
    android:id="@+id/map"
    android:background="@drawable/address"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</com.xxx.map.MapView>

How do i retrieve properties in the constructor from AttributeSet ? Let's say the drawable in the background field.

public MapView(Context context, AttributeSet attrs) {
}

解决方案

In the general case, you do like this:

public MapView(Context context, AttributeSet attrs) {
    // ...

    int[] attrsArray = new int[] {
        android.R.attr.id, // 0
        android.R.attr.background, // 1
        android.R.attr.layout_width, // 2
        android.R.attr.layout_height // 3
    };
    TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);
    int id = ta.getResourceId(0 /* index of attribute in attrsArray */, View.NO_ID);
    Drawable background = ta.getDrawable(1);
    int layout_width = ta.getDimensionPixelSize(2, ViewGroup.LayoutParams.MATCH_PARENT);
    int layout_height = ta.getDimensionPixelSize(3, ViewGroup.LayoutParams.MATCH_PARENT);
    ta.recycle();
}

Pay attention to how the indexes of the elements in in attrsArray matter. However, in your particular case, it works just as good to use the getters, like you discovered yourself:

public MapView(Context context, AttributeSet attrs) {
    super(context, attrs); // After this, use normal getters

    int id = this.getId();
    Drawable background = this.getBackground();
    ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
}

This works because the attribute you have on com.xxx.map.MapView are basic attributes that the View base class parses in its constructor. If you want to define your own attributes, take a look at this question and the excellent answer: Declaring a custom android UI element using XML

这篇关于如何获得的AttributeSet属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆