[英]Change spinner background color for specific item
我有個問題 。 是否可以為特定項目設置特定的背景顏色? 。 例如,我想為項目“ DLUGOSC”設置“黃色”。 以下是我的strings.xml和微調框布局。 謝謝
**strings.xml**
<resources>
<string name="app_name">Konwerter Jednostek</string>
<string name="action_settings">Settings</string>
<string-array name="units">
<item><b>DLUGOSC</b></item>
<item>Cal</item>
<item>Centymetr</item>
<item>Stopa</item>
<item>Jard</item>
<item>Metr</item>
<item>Mila</item>
<item>Kilometr</item>
<item>Decymetr</item>
<item>Milimetr</item>
</string-array>
微調器布局
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner_from"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
/>
對的,這是可能的。 在這里,我強調“ Cal”。
List<String> units = Arrays.asList(getResources().getStringArray(R.array.units));
Spinner spinner = (Spinner) findViewById(R.id.spinner_from);
spinner.setAdapter(new TestAdapter(units , this);
public class TestAdapter extends BaseAdapter {
List<String> strings;
Context context;
public TestAdapter(List<String> stringList, Context context) {
strings = stringList;
this.context = context;
}
@Override
public int getCount() {
return strings.size();
}
@Override
public String getItem(int position) {
return strings.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(context);
textView.setText(strings.get(position));
//here you can use position or string
if(position == 1 || strings.get(position).equals("Cal")) {
textView.setBackgroundColor(Color.RED);
}
return textView;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.