好的,我正在尝试制作一个自定义微调器,只使用图像。我在 values 文件夹中的 .xml 文件上定义了一个数组,其中有图像项作为资源。我想用这些图像填充微调器。这是我到目前为止所做的一切:
在 res/drawable-mdpi
我有以下图片:
remove_minus_circle_black.png
remove_minus_circle_red.png
remove_minus_sign.png
remove_minus_sign2.png
remove_minus_sign3.png
在 res/values/arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="minusIcons">
<item>@drawable/remove_minus_circle_black</item>
<item>@drawable/remove_minus_circle_red</item>
<item>@drawable/remove_minus_sign</item>
<item>@drawable/remove_minus_sign2</item>
<item>@drawable/remove_minus_sign3</item>
</array>
<array name="directionIcons">
</array>
</resources>
在 res/layout/row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
现在我尝试使用自定义适配器将图像添加到微调器的代码(活动本身):
public class Definicoes extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_definicoes);
Spinner spinner = (Spinner) findViewById(R.id.minusSpinner);
int[] array = getResources().getIntArray(R.array.minusIcons);
String [] objects = new String[array.length];
for(int i = 0; i != array.length; i++){
objects[i] = "" + array[i];
}
spinner.setAdapter(new MyAdapter(Definicoes.this, R.id.minusSpinner, objects));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.definicoes, menu);
return true;
}
class MyAdapter extends ArrayAdapter<String>{
String[] objects;
public MyAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
icon.setImageResource(Integer.parseInt(objects[position]));
return row;
}
}
}
问题/错误:我打开活动,然后单击微调器,那里什么也没有!非常感谢你们!我想我可能做错了什么,从未使用过自定义适配器...