是否可以为每个微调器项目赋予不同的颜色



在警报对话框中,我有一个带有颜色代码的微调器:

Spinner cSpinner = (Spinner)mView.findViewById(R.id.spinner_c);
ArrayAdapter<String> cadapter = new ArrayAdapter<String>(Cal_main.this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.colorspin));
cadapter.setDropDownViewResource(R.layout.custom_spinner_items);
cSpinner.setAdapter(cadapter);

颜色阵列:

<string-array name="colorspin">
<item>#f44336</item>
<item>#d32f2f</item>
<item>#e91e63</item>
<item>#c2185b</item>
<item>#9c27b0</item>
</string-array>

如果可能的话,将每个项目的背景设置为数组颜色的最佳方式是什么?

是的。

阵列和微调器:

String[] colors = getResources().getStringArray(R.array.colorspin);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) mView.findViewById(R.id.spinner_c);
SpinnerColorAdapter spincAdapter = new SpinnerColorAdapter(getApplicationContext(),colors);
spin.setAdapter(spincAdapter);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
String color = colors[position].toString();
tvStep.setText(color);
Toast.makeText(getApplicationContext(), color, Toast.LENGTH_LONG).show();
}

SpinnerColorAdapter:

public class SpinnerColorAdapter extends BaseAdapter {
Context context;
String[] Colors;
LayoutInflater inflter;
public SpinnerColorAdapter(Context applicationContext,  String[] Colors) {
this.context = applicationContext;
this.Colors = Colors;
inflter = (LayoutInflater.from(applicationContext));
}
//@Override
public int getCount() {
return Colors.length;
}
//@Override
public Object getItem(int i) {
return null;
}
//@Override
public long getItemId(int i) {
return 0;
}
//@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.spinner_row, null);
Button colorbtn = (Button) view.findViewById(R.id.btn_indexcolor);
colorbtn.setBackgroundColor(Color.parseColor(Colors[i]));
TextView colorview = (TextView) view.findViewById(R.id.indexcolor);
return view;
}
}

相关内容

  • 没有找到相关文章

最新更新