【问题标题】:WORKS: Android Custom Component - access Array in strings.xml, supplied via layout.xml作品:Android 自定义组件 - 访问 strings.xml 中的数组,通过 layout.xml 提供
【发布时间】:2012-11-24 16:55:48
【问题描述】:

我有一个扩展LinearLayout的类,里面有Buttons和一个Spinner

此对象通过我的布局 XML 文件包含:

<com.ics.spinn.ComboBox android:id="@+id/myautocombo"
  android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:entries="@array/suppliers" />
/>

数组供应商在strings.xml中定义。

如果这个组件现在不是 com.ics.spinn.ComboBox,而是Spinner,Android 会 将“android:entries”自动填充到 Spinner 适配器。

我希望我的组件 com.ics.spinn.ComboBox 的行为方式相同: 能够访问通过 xml 文件定义的数组,所以我可以 通过以下方式将其提供给我的组件内的 Spinner:

    ArrayAdapter<String> a = new ArrayAdapter<String>(this.getContext(),android.R.layout.simple_spinner_dropdown_item, ARRAYINSIDEMYXML);
    s.setAdapter(a);

我现在可以通过 getResources().getStringArray(R.array.suppliers) 直接访问 strings.xml 中定义的数组 但我的代码不应该知道“供应商”这个名字,因为它应该通过 android:entries...

这 + João Melo 解决方案 WORK 中 xml 中的条目:

        public ComboBox(Context context, AttributeSet attrs) {
        super(context, attrs);

          TypedArray b = context.obtainStyledAttributes(attrs,
                    R.styleable.ComboBox, 0, 0);

            CharSequence[] entries = b.getTextArray(R.styleable.ComboBox_myEntries);
            if (entries != null) {
                ArrayAdapter<CharSequence> adapter =
                        new ArrayAdapter<CharSequence>(context,
                                android.R.layout.simple_spinner_item, entries);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                s.setAdapter(adapter);
            }
}

【问题讨论】:

  • 您的 TypedArray 是在 res/values 文件夹内的 attrs.xml 文件中定义的。在下面检查我的答案。

标签: android xml arrays components


【解决方案1】:

尝试像这样加载你的数组:

String[] array = getResources().getStringArray(R.array.recipes_string_array);

ArrayAdapter spinnerAdapter = new ArrayAdapter<String>(getActivity(), R.layout.simple_spinner_dropdown_item) {

    @Override
    public int getCount() {
       return array.length;
    }

    @Override
        public String getItem(int position) {
        return array[position];
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Create your item for the spinner and return it         
        return spinnerItemview; 
    }

}

spinner.setAdapter(spinnerAdapter);

【讨论】:

  • 他正在尝试从自定义组件的android:entries 属性中获取数组。
【解决方案2】:

我不知道是否可以使用 android:entries 属性来做到这一点,除非您的组件扩展了 Spinner,但我只是猜测。

您可以在 attrs.xml 中创建自己的自定义属性

<declare-styleable name="ComboBox">
    <attr name="myEntries" format="reference"></attr>
</declare-styleable>

然后您可以在组件中访问此引用 (int) 并将 ArrayAdapter 设置到您的微调器中。

TypedArray customAttrs = context.obtainStyledAttributes(attrs, R.styleable.ComboBox);
    for (int i = 0; i < customAttrs.length(); i++) {
        int attrValue = customAttrs.getIndex(i);
        switch (attrValue) {
            case R.styleable.ComboBox_myEntries:
                mArrayId = customAttrs.getResourceId(attrValue, 0);
                ArrayAdapter<String> a = new ArrayAdapter<String>(this.getContext(),android.R.layout.simple_spinner_dropdown_item, mArrayId);
                s.setAdapter(a);
                break;
        }
    }

在您的布局中,在根视图的 xmlns:android="http://schemas.android.com/apk/res/android" 下方添加这一行 xmlns:app="http://schemas.android.com/apk/res/yourPackageName"

然后你可以通过 xml 实例化你的组件和自定义属性:

<com.ics.spinn.ComboBox android:id="@+id/myautocombo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
app:myEntries="@array/suppliers" />
/>

不知道这个答案是否正是您想要的,但它的行为就像android:entries。希望对您有所帮助。

【讨论】:

  • 谢谢,在您的帮助下,我能够解决问题,见上文。唯一令人沮丧的是 attr.xml + 中的条目,它不被称为 android:entries...
【解决方案3】:

Joala 的上述回答在技术上是正确的,但有一种更简单的表达方式。

您可以直接询问 StringArray 的 resourceId,而不是遍历 StyledAttributes。

// Get the DisplayValues from the XML config.
final TypedArray customAttrs = getContext().obtainStyledAttributes(attrs, R.styleable.ComboBox);
final int resourceId = customAttrs.getResourceId(R.styleable.ComboBox_myEntries, -1);
if (resourceId == -1) {
    throw new IllegalArgumentException("ComboBox requires a myEntries attribute that points to a string-array resource");
}

final ArrayAdapter<String> a = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_spinner_dropdown_item, resourceId);
s.setAdapter(a);

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 2011-07-19
    • 1970-01-01
    相关资源
    最近更新 更多