您好,登錄后才能下訂單哦!
要實現Spinner中選項的圖標與文本并排,可以使用自定義適配器來處理Spinner中每個選項的布局。以下是一個示例代碼:
public class CustomAdapter extends ArrayAdapter<String> {
private Context mContext;
private List<String> mOptions;
private List<Integer> mIcons;
public CustomAdapter(Context context, int resource, List<String> objects, List<Integer> icons) {
super(context, resource, objects);
mContext = context;
mOptions = objects;
mIcons = icons;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.spinner_row, parent, false);
TextView textView = row.findViewById(R.id.text_view);
ImageView imageView = row.findViewById(R.id.image_view);
textView.setText(mOptions.get(position));
imageView.setImageResource(mIcons.get(position));
return row;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getView(position, convertView, parent);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"/>
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
List<String> options = Arrays.asList("Option 1", "Option 2", "Option 3");
List<Integer> icons = Arrays.asList(R.drawable.ic_option1, R.drawable.ic_option2, R.drawable.ic_option3);
Spinner spinner = findViewById(R.id.spinner);
CustomAdapter adapter = new CustomAdapter(this, R.layout.spinner_row, options, icons);
spinner.setAdapter(adapter);
通過以上步驟,你可以實現Spinner中選項的圖標與文本并排顯示。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。