作品:Android自定义组件-访问通过layout.xml提供的strings.xml中的数组 [英] WORKS: Android Custom Component - access Array in strings.xml, supplied via layout.xml
问题描述
我有一个类,扩展了 LinearLayout
,其中有 Buttons
和一个 Spinner
。
I have class, which extends LinearLayout
, in it there are Buttons
and a Spinner
.
此对象通过我的布局XML文件包含:
This Object gets included via my layout XML file:
<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中定义的。
The array suppliers is defined in strings.xml.
如果此组件现在不是com.ics.spinn.ComboBox,而是 Spinner
,则Android将
自动填充 android:entries
到Spinner适配器。
If this component now wouldn't be com.ics.spinn.ComboBox, but a Spinner
, Android would
auto-populate the "android:entries
" to the Spinner adapter.
我希望组件com.ics.spinn.ComboBox正常运行同样的方法:
能够访问通过xml文件定义的数组,因此我可以
通过以下方式将其提供给组件内部的Spinner:
I'd like my component com.ics.spinn.ComboBox to behave the same way: to be able to access the array defined via the xml file, so I can supply it to the Spinner inside my component, via:
ArrayAdapter<String> a = new ArrayAdapter<String>(this.getContext(),android.R.layout.simple_spinner_dropdown_item, ARRAYINSIDEMYXML);
s.setAdapter(a);
我现在可以通过 getResources直接访问strings.xml中定义的数组().getStringArray(R.array.suppliers)
,但我的代码不应该知道供应商的名称,因为它应通过android:entries提供...
I now I could access the array defined in strings.xml DIRECTLY via getResources().getStringArray(R.array.suppliers)
but my code shouldn't know of the name "suppliers", since it shall be supplied via android:entries...
这+JoãoMelo解决方案中xml中的条目:
This + the entries in xml in João Melo solution WORK:
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);
}
}
推荐答案
I不知道是否有可能使用 android:entries
属性执行此操作,除非您的组件扩展了Spinner,但我只是在猜测。
I don't know if it's possible to do that with android:entries
attribute unless your component extends Spinner, but I'm only guessing.
您可以实现在attrs.xml中创建自己的自定义属性
You can achieve that creating your own custom attribute in attrs.xml
<declare-styleable name="ComboBox">
<attr name="myEntries" format="reference"></attr>
</declare-styleable>
然后,您可以在组件内部访问此引用(int),并将ArrayAdapter设置为微调器。 / p>
Then you can access this reference (int) inside your component and set the ArrayAdapter into your spinner.
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
在根视图中:
On your layout, add this line xmlns:app="http://schemas.android.com/apk/res/yourPackageName"
below xmlns:android="http://schemas.android.com/apk/res/android"
in the root view:
然后,您可以通过xml实例化组件和自定义属性:
Then you can instantiate your component and custom attrs via 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
。希望对您有所帮助。
Don't know if this answer is exactly what you're looking for but it would behave just like android:entries
. Hope it helps.
这篇关于作品:Android自定义组件-访问通过layout.xml提供的strings.xml中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!