如何自定义Spinner下拉视图

Str*_*rry 7 android textview android-spinner

是否可以自定义微调器下拉视图.默认微调器下拉视图具有适配器视图.我希望更改该视图以拥有自己的文本视图或类似的东西.

Ant*_*des 5

在您的班级中添加此内部类,然后根据需要对其进行修改。

public class MyAdapter extends ArrayAdapter<String>{

        public MyAdapter(Context context, int textViewResourceId,   String[] objects) {
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.xml.row, parent, false);
            TextView label=(TextView)row.findViewById(R.id.company);
            label.setText(strings[position]);

            TextView sub=(TextView)row.findViewById(R.id.sub);
            sub.setText(subs[position]);

            ImageView icon=(ImageView)row.findViewById(R.id.image);
            icon.setImageResource(arr_images[position]);

            return row;
            }
        }
Run Code Online (Sandbox Code Playgroud)

并将此适配器添加到微调器中:

    Spinner mySpinner = (Spinner)findViewById(R.id.expandableImagesList);
    mySpinner.setAdapter(new MyAdapter(NewEventActivity.this, R.xml.row, strings));
Run Code Online (Sandbox Code Playgroud)

并创建一个新的xml并添加您希望微调器包含的所有内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:padding="3dip"
>
    <ImageView
         android:id="@+id/image"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/icon"/>
    <TextView
         android:layout_toRightOf="@+id/image"
         android:padding="3dip"
         android:layout_marginTop="2dip"
         android:textStyle="bold"
         android:id="@+id/company"
         android:text="CoderzHeaven"
         android:layout_marginLeft="5dip"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
     <TextView
         android:layout_toRightOf="@+id/image"
         android:padding="2dip"
         android:layout_marginLeft="5dip"
         android:id="@+id/sub"
         android:layout_below="@+id/company"
         android:text="Heaven of all working codes"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是我的代码,因此您必须根据需要进行更改


waq*_*lam 1

getDropDownView您可以通过覆盖ArrayAdapter.

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
     ...
}
Run Code Online (Sandbox Code Playgroud)