使用数组引用作为自定义android视图的XML属性

Mic*_*ael 29 android custom-attributes android-custom-view android-xml

此问题已解决,请参阅注释以获取详细信息.

我正在扩展现有的Android View并加载一些自定义属性,如使用XML声明自定义Android UI元素定义自定义attrs中所述.

具有布尔和整数格式的属性可以正常工作,但是当我尝试指定对数组资源的引用时,应用程序在启动时崩溃.我在xml资源文件中定义了一个整数数组,我试图将它用作自定义视图的属性.

我可以使用数组资源来设置android Spinner类的"entries"属性而没有错误,所以它似乎是我的实现中的一个问题.logcat消息似乎没有提供有关崩溃的任何具体信息,但我仍然在寻找,所以如果我找到了什么,我会更新.

属性由(在attrs.xml中)声明:

<declare-styleable name="CustomView">
    <attr name="values" format="reference"/>
    <attr name="isActive" format="boolean"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

该数组定义为(在arrays.xml中):

<integer-array name="nums">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</integer-array>
Run Code Online (Sandbox Code Playgroud)

我通过以下方式引用数组:

<com.test.CustomView cv:values="@array/nums" />
Run Code Online (Sandbox Code Playgroud)

这会导致应用程序立即崩溃.此外,如果我引用颜色资源而不是数组,那么应用程序不会崩溃.有人知道如何处理这个问题吗?

Xåp*_* - 43

只是在这里捎带你的问题,因为你的帖子首先出现,如果你谷歌像"数组参考XML属性自定义视图",所以有人可能会发现这有用.

如果希望自定义视图引用字符串数组,则可以使用Android现有的android:entriesXML属性,而不是创建全新的自定义属性.

只需执行以下操作res/values/attrs.xml:

<resources>
    <declare-styleable name="MyCustomView">
        <attr name="android:entries" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

然后在自定义View的构造函数中执行此操作:

public MyCustomView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);

    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle, 0);
    try
    {
        CharSequence[] entries = a.getTextArray(R.styleable.MyCustomView_android_entries);
        if (entries != null)
        {
           //do something with the array if you want
        }
    }
    finally
    {
        a.recycle();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,android:entries当您将自定义视图添加到XML布局文件时,您应该能够通过该属性引用字符串数组.例:

<com.example.myapp.MyCustomView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:entries="@array/my_array_of_strings" />
Run Code Online (Sandbox Code Playgroud)

这正是它在ListView类中的完成方式(在源代码中查看,你会看到).

  • 我想通过一个可绘制数组作为参数..任何帮助 (2认同)

Jin*_*Jin 15

另一个答案适用于字符串数组.但是,arr.getTextArray(...)参考数组上,例如

<array name="tmp">
  <item>@drawable/a</item>
  <item>@drawable/b</item>
</array>
Run Code Online (Sandbox Code Playgroud)

将为您res/drawable/a.png提供CharSequence而不是资源ID.

解析引用数组的正确方法是:

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView);

int arrayResourceId = typedArray.getResourceId(R.styleable.CustomView_CustomAttr, 0);
if (arrayResourceId != 0) {
    final TypedArray resourceArray = getResources().obtainTypedArray(arrayResourceId);
    for (int i = 0; i < resourceArray.length(); i++) {
        final int resourceId = resourceArray.getResourceId(i, 0);

        // do stuff with resourceId, such as getResources().getDrawable(resourceId)
    }
    resourceArray.recycle();
}
typedArray.recycle();
Run Code Online (Sandbox Code Playgroud)


Liu*_*NO. 5

问题是关于获取整数数组,对于我来说,我需要从数组中读取针对我的自定义视图的colors(int),如下所示:

<declare-styleable name="ColorPickerView">
        <attr name="colors" format="reference" />
    </declare-styleable>
Run Code Online (Sandbox Code Playgroud)

然后使用如下所示的自定义视图:

<com.rainliu.colorpicker.ColorPickerView
    android:id="@+id/rtePalette"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    colorPickerView:colors="@array/colorPickerColors"
    />
Run Code Online (Sandbox Code Playgroud)

颜色定义如下:

<resources>
    <color name="colorPrimary">#FF9800</color>
    <array name="colorPickerColors">
        <item>#000000</item>
        <item>#E65100</item>
        <item>@color/colorPrimary</item>
    </array>
</resources>
Run Code Online (Sandbox Code Playgroud)

所以我需要在我的自定义视图(ColorPickerView)中获取颜色,代码如下:

TypedArray ta = context.obtainStyledAttributes(attributeSet, R.styleable.ColorPickerView);
int colorsId = ta.getResourceId(R.styleable.ColorPickerView_colors, 0);
int[] colorsArray = ta.getResources().getIntArray(colorsId);
for (int a : colorsArray) {
  Log.e("AA", "color == " + a);
}
ta.recycle();
Run Code Online (Sandbox Code Playgroud)

这是colorsArray的打印结果:

03-11 14:25:53.894 15300-15300/com.chinalwb.are E/AA: color == -16777216
03-11 14:25:53.894 15300-15300/com.chinalwb.are E/AA: color == -1683200
03-11 14:25:53.894 15300-15300/com.chinalwb.are E/AA: color == -1683200
Run Code Online (Sandbox Code Playgroud)

希望这对一些人有帮助。