您好,登錄后才能下訂單哦!
要自定義Spinner控件的列表項(xiàng)背景色,可以通過自定義Adapter來實(shí)現(xiàn)。下面是一個(gè)簡(jiǎn)單的例子:
public class CustomAdapter extends ArrayAdapter<String> {
private Context mContext;
private int mResource;
private List<String> mItems;
private int mBackgroundColor;
public CustomAdapter(Context context, int resource, List<String> items, int backgroundColor) {
super(context, resource, items);
mContext = context;
mResource = resource;
mItems = items;
mBackgroundColor = backgroundColor;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(mResource, parent, false);
TextView textView = view.findViewById(android.R.id.text1);
textView.setText(mItems.get(position));
textView.setBackgroundColor(mBackgroundColor);
return view;
}
}
List<String> items = new ArrayList<>();
items.add("Item 1");
items.add("Item 2");
items.add("Item 3");
Spinner spinner = findViewById(R.id.spinner);
int backgroundColor = Color.parseColor("#FF0000"); // 設(shè)置背景色為紅色
CustomAdapter adapter = new CustomAdapter(this, android.R.layout.simple_spinner_item, items, backgroundColor);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
通過以上步驟,就可以自定義Spinner控件的列表項(xiàng)背景色了??梢愿鶕?jù)自己的需求修改Adapter中的代碼,來實(shí)現(xiàn)更多個(gè)性化的效果。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。