如何获取 AttributeSet 属性

how to get AttributeSet properties(如何获取 AttributeSet 属性)
本文介绍了如何获取 AttributeSet 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

假设我有一个扩展 ViewGroup 的类

Suppose i have a class which extends ViewGroup

public class MapView extends ViewGroup

它包含在布局中 map_controls.xml 像这样

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>

如何从 AttributeSet 检索构造函数中的属性?假设背景字段中的可绘制对象.

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. getLayoutDimension(2, ViewGroup.LayoutParams.MATCH_PARENT);
    int layout_height = ta. getLayoutDimension(3, ViewGroup.LayoutParams.MATCH_PARENT);
    ta.recycle();
}

注意 attrsArray 中元素的索引如何影响.但是,在您的特定情况下,使用 getter 一样好,就像您自己发现的那样:

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();
}

这是因为您在 com.xxx.map.MapView 上拥有的属性是 View 基类在其构造函数中解析的基本属性.如果您想定义自己的自己的属性,请查看这个问题和出色的答案:使用 XML 声明一个自定义的 android UI 元素

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 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum(Android 发布 APK 因 java.lang.AssertionError 崩溃:在 java.lang.Enum 中不可能)
Finished with Non Zero Exit Value 3(以非零退出值 3 结束)
On gradle:3.0.0 More than one file was found with OS independent path #39;META-INF/ASL2.0#39;(在 gradle:3.0.0 上找到多个文件,其独立于操作系统的路径为“META-INF/ASL2.0)
Android : app loading library at runtime on Lollipop but not IceCreamSandwich(Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上的应用程序加载库)
buildConfigField depending on flavor + buildType(buildConfigField 取决于风味 + buildType)
How do I suppress warnings when compiling an android library with gradle?(使用 gradle 编译 android 库时如何抑制警告?)