您好,登錄后才能下訂單哦!
你可以使用以下方法來實現Spinner控件的列表項背景漸變色:
public class CustomSpinnerAdapter extends ArrayAdapter<String> {
private Context mContext;
private List<String> mData;
public CustomSpinnerAdapter(Context context, int resource, List<String> data) {
super(context, resource, data);
this.mContext = context;
this.mData = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
// 設置背景漸變色
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{Color.RED, Color.BLUE});
view.setBackground(gradientDrawable);
TextView textView = view.findViewById(android.R.id.text1);
textView.setText(mData.get(position));
return view;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
// 設置背景漸變色
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{Color.RED, Color.BLUE});
view.setBackground(gradientDrawable);
TextView textView = view.findViewById(android.R.id.text1);
textView.setText(mData.get(position));
return view;
}
}
Spinner spinner = findViewById(R.id.spinner);
List<String> data = new ArrayList<>();
data.add("Item 1");
data.add("Item 2");
data.add("Item 3");
CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(this, android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
通過以上方法,你可以實現Spinner控件的列表項背景漸變色效果。需要注意的是,你可以根據實際需求自定義漸變色的顏色和方向。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。