Custom layout for Spinner item

后端 未结 1 1462
星月不相逢
星月不相逢 2020-11-30 12:56

I have a spinner within alert dialog. I wanted to reduce padding between spinner items and hence I implemented following:

spinner_row.xml



        
相关标签:
1条回答
  • 2020-11-30 13:28

    I have this

    enter image description here

    and this

    enter image description here

    with these code of xml

    xml for adapter named spinadapt.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:background="#fff" >
    
    <TextView
        android:id="@+id/tvCust"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_toLeftOf="@+id/radioButton1"
        android:gravity="left|center_vertical"
        android:textColor="#000"
        android:textSize="15sp" />
    
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignParentRight="true" />
    
    </RelativeLayout>
    

    and main layout named activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" 
        android:hint="Select item"
        android:background="@drawable/spin"/>
    
    </RelativeLayout>
    

    and java code is class named MainActivity.java

    public class MainActivity extends Activity
    {
        Spinner sp;
        TextView tv;
        String[]  counting={"One","Two","Three","Four"};
        @Override
        protected void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                sp=new Spinner(this);
                tv=(TextView)findViewById(R.id.spinner1);
                tv.setOnClickListener(new OnClickListener()
                    {                       
                        @Override
                        public void onClick(View v)
                            {
                                sp.performClick();
                            }
                    });
                sp.setAdapter(new Adapter(MainActivity.this, counting));
                sp.setOnItemSelectedListener(new OnItemSelectedListener()
                    {
                        @Override
                        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
                            {
                                tv.setText(counting[arg2]);
                            }
                        @Override
                        public void onNothingSelected(AdapterView<?> arg0)
                            {
                            }
                    });
            }
    }
    

    and adapter class named Adapter.java

    public class Adapter extends BaseAdapter
    {
        LayoutInflater inflator;
        String[] mCounting;
    
        public Adapter( Context context ,String[] counting)
            {
                inflator = LayoutInflater.from(context);
                mCounting=counting;
            }
    
        @Override
        public int getCount()
            {
                return mCounting.length;
            }
    
        @Override
        public Object getItem(int position)
            {
                return null;
            }
    
        @Override
        public long getItemId(int position)
            {
                return 0;
            }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
            {
                convertView = inflator.inflate(R.layout.spinadapt, null);
                TextView tv = (TextView) convertView.findViewById(R.id.tvCust);
                tv.setText(Integer.toString(position));
                return convertView;
            }
    }
    

    this is working perfect

    hope this will help

    0 讨论(0)
提交回复
热议问题