鉴于下面的代码,是否可以在阵列行星中使用图像而不是文本?
Spinner s = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.planets, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
和
<string name="planet_prompt">Choose a planet</string>
<string-array name="planets">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
答案 0 :(得分:20)
是的,请查看以下代码:
array of data
// stores the image database icons
private static Integer[] imageIconDatabase = { R.drawable.ball,
R.drawable.catmouse, R.drawable.cube, R.drawable.fresh,
R.drawable.guitar, R.drawable.orange, R.drawable.teapot };
// stores the image database names
private String[] imageNameDatabase = { "ball", "catmouse", "cube", "fresh",
"guitar", "orange", "teapot" };
创建散列图列表
private void initializeImageList() {
// TODO Auto-generated method stub
for (int i = 0; i < imageNameDatabase.length; i++) {
map = new HashMap<String, Object>();
map.put("Name", imageNameDatabase[i]);
map.put("Icon", imageIconDatabase[i]);
spinnerList.add(map);
}
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource((spinnerList.get(0).get("Icon"));
spinnerList.get(0).get("Name");
}
将微调器分配给适配器
public void createAddDialog() {
// TODO
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list_editoradd_dialog);
Spinner spin = (Spinner) findViewById(R.id.spinnerAddImageList);
CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(this,
spinnerData, R.layout.spinner_view, new String[] { "Name",
"Icon" }, new int[] { R.id.imageNameSpinner,
R.id.imageIconSpinner });
spin.setAdapter(adapter);
}
上面使用的适配器如下所示..
package com.tcs.CustomListViewGeneration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
class CustomSpinnerAdapter extends SimpleAdapter {
LayoutInflater mInflater;
private List<? extends Map<String, ?>> dataRecieved;
public CustomSpinnerAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
dataRecieved =data;
mInflater=LayoutInflater.from(context);
}
@SuppressWarnings("unchecked")
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.spinner_view,
null);
}
// HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
((TextView) convertView.findViewById(R.id.imageNameSpinner))
.setText((String) dataRecieved.get(position).get("Name"));
((ImageView) convertView.findViewById(R.id.imageIconSpinner))
.setBackgroundResource(dataRecieved.get(position).get("Icon")));
return convertView;
}
}
答案 1 :(得分:17)
我只是需要一个超级简单的解决方案,在旋转器中固定一组图像,所以我这样做了:
public class SimpleImageArrayAdapter extends ArrayAdapter<Integer> {
private Integer[] images;
public SimpleImageArrayAdapter(Context context, Integer[] images) {
super(context, android.R.layout.simple_spinner_item, images);
this.images = images;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getImageForPosition(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getImageForPosition(position);
}
private View getImageForPosition(int position) {
ImageView imageView = new ImageView(getContext());
imageView.setBackgroundResource(images[position]);
imageView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
return imageView;
}
}
然后在代码中你可以像这样使用它:
SimpleImageArrayAdapter adapter = new SimpleImageArrayAdapter(context,
new Integer[]{R.drawable.smiley1, R.drawable.smiley2, R.drawable.smiley3, R.drawable.smiley4, R.drawable.smiley5});
spinner.setAdapter(adapter);
答案 2 :(得分:0)
不确定在2020年是否还会有人使用Spinners,但这是此解决方案的Kotlin版本(https://stackoverflow.com/a/22216375/6007104):
class MySpinnerAdapter(context: Context, images: Array<Int>) :
ArrayAdapter<Int>(context, android.R.layout.simple_spinner_item, images) {
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup) =
getImageForPosition(position)
override fun getView(position: Int, convertView: View?, parent: ViewGroup) =
getImageForPosition(position)
private fun getImageForPosition(position: Int) = ImageView(context).apply {
setBackgroundResource(getItem(position)!!)
layoutParams = ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
}
}
答案 3 :(得分:0)
上述使用 SimpleImageArrayAdapter 的解决方案完美运行,除非 Android 版本为 Marshmallow 或更旧版本,在这种情况下,您必须使用带有原始问题中指定的字符串数组的 ArrayAdapter,否则您的下拉列表将是一个空的白框.