1

我有一个类,它扩展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);

我现在可以通过直接访问 strings.xml 中定义的数组,getResources().getStringArray(R.array.suppliers) 但我的代码不应该知道名称“供应商”,因为它应该通过 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);
            }
}
4

3 回答 3

1

我不知道是否可以使用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:app="http://schemas.android.com/apk/res/yourPackageName"以下行:xmlns:android="http://schemas.android.com/apk/res/android"

然后你可以通过 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. 希望能帮助到你。

于 2012-12-06T16:41:27.073 回答
0

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-01-07T11:21:13.650 回答
0

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

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);
于 2012-12-06T16:10:49.813 回答