Change Spinner dropdown icon

后端 未结 9 2147
旧巷少年郎
旧巷少年郎 2020-12-12 14:07

The solutions I found to change the spinner dropdown icon where all:

1. create a custom drawable



        
相关标签:
9条回答
  • 2020-12-12 14:48

    dummy.xml(remember image size should be less)

    <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
                <layer-list android:opacity="transparent">
                    <item android:width="60dp" android:gravity="left" android:start="20dp">
                        <bitmap  android:src="@drawable/down_button_dummy_dummy" android:gravity="left"/>
                    </item>
                </layer-list>
            </item>
        </selector>
    

    layout file snippet be like

    <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardUseCompatPadding="true"
            app:cardElevation="5dp"
            >
         <Spinner
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@drawable/dummy">
    
         </Spinner>
        </android.support.v7.widget.CardView>
    

    click to see result layout image

    0 讨论(0)
  • 2020-12-12 14:56

    Add theme to spinner

    <Spinner style="@style/SpinnerTheme"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>
    

    Add spinnerTheme to styles.xml

    <style name="SpinnerTheme" parent="android:Widget.Spinner">
        <item name="android:background">@drawable/spinner_background</item>
    </style>
    

    Add New -> "Vector Asset" to drawable with e.g. "ic_keyboard_arrow_down_24dp"

    Add spinner_background.xml to drawable

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <layer-list>
                <item android:drawable="@drawable/ic_keyboard_arrow_down_24dp" android:gravity="center_vertical|right" android:right="5dp"/>
            </layer-list>
        </item>
    </selector>
    
    0 讨论(0)
  • 2020-12-12 14:57

    I have had a lot of difficulty with this as I have a custom spinner, if I setBackground then the Drawable would stretch. My solution to this was to add a drawable to the right of the Spinner TextView. Heres a code snippet from my Custom Spinner. The trick is to Override getView and customize the Textview as you wish.

    public class NoTextSpinnerArrayAdapter extends ArrayAdapter<String> {
    
        private String text = "0";
    
        public NoTextSpinnerArrayAdapter(Context context, int textViewResourceId, List<String> objects) {
            super(context, textViewResourceId, objects);
        }
    
        public void updateText(String text){
            this.text = text;
            notifyDataSetChanged();
        }
    
        public String getText(){
            return text;
        }
    
        @NonNull
        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView textView = view.findViewById(android.R.id.text1);
            textView.setCompoundDrawablePadding(16);
            textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_menu_white_24dp, 0);
    
            textView.setGravity(Gravity.END);
            textView.setText(text);
            return view;
        }
    }
    

    You also need to set the Spinner background to transparent:

    <lifeunlocked.valueinvestingcheatsheet.views.SelectAgainSpinner
                android:id="@+id/saved_tickers_spinner"
                android:background="@android:color/transparent"
                android:layout_width="60dp"
                android:layout_height="match_parent"
                tools:layout_editor_absoluteX="248dp"
                tools:layout_editor_absoluteY="16dp" />
    

    and my custom spinner if you want it....

    public class SelectAgainSpinner extends android.support.v7.widget.AppCompatSpinner {
        public SelectAgainSpinner(Context context) {
            super(context);
        }
    
        public SelectAgainSpinner(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public SelectAgainSpinner(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public void setPopupBackgroundDrawable(Drawable background) {
            super.setPopupBackgroundDrawable(background);
        }
    
        @Override
        public void setSelection(int position, boolean animate) {
            boolean sameSelected = position == getSelectedItemPosition();
            super.setSelection(position, animate);
            if (sameSelected) {
                // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
                if (getOnItemSelectedListener() != null) {
                    getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
                }
            }
        }
    
        @Override
        public void setSelection(int position) {
            boolean sameSelected = position == getSelectedItemPosition();
            super.setSelection(position);
            if (sameSelected) {
                // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
                if (getOnItemSelectedListener() != null) {
                    getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-12 15:00

    Try applying following style to your spinner using

    style="@style/SpinnerTheme"
    

    //Spinner Style:

    <style name="SpinnerTheme" parent="android:Widget.Spinner">
        <item name="android:background">@drawable/bg_spinner</item>
    </style>
    

    //bg_spinner.xml Replace the arrow_down_gray with your arrow

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item>
    
            <layer-list>
    
                <item>
                    <shape>
                        <gradient android:angle="90" android:endColor="#ffffff" android:startColor="#ffffff" android:type="linear" />
    
                        <stroke android:width="0.33dp" android:color="#0fb1fa" />
    
                        <corners android:radius="0dp" />
    
                        <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
                    </shape>
                </item>
    
                <item android:right="5dp">
    
                    <bitmap android:gravity="center_vertical|right" android:src="@drawable/arrow_down_gray" />
    
                </item>
    
            </layer-list>
    
        </item>
    
    </selector>
    
    0 讨论(0)
  • 2020-12-12 15:03

    Without Using ANY Drop down Using your Drop Down ICON

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item>
        <layer-list>
            <item>
                <shape>
                   <gradient android:angle="90" android:endColor="#ffffff" android:startColor="#ffffff" android:type="linear" /><!--For gradient background-->
    
                    <stroke android:width="1dp" android:color="#FFF" /><!--For Border background-->
    
                    <corners android:radius="0dp" /><!--For background corner-->
    
                    <padding android:bottom="3dp" android:left="3dp" android:right="6dp" android:top="3dp" /><!--For padding for all sides-->
                </shape>
            </item>
            <item>
                <bitmap android:gravity="center|right" android:src="@drawable/ic_down_arrow" /> // Replace with your Icon
    
            </item>
        </layer-list>
    </item>
    

    0 讨论(0)
  • 2020-12-12 15:07

    We can manage it by hiding the icon as i did:

    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <Spinner android:id="@+id/fragment_filter_sp_users"
                 android:layout_width="match_parent"
                 android:background="@color/colorTransparent"
                 android:layout_height="wrap_content"/>
    
        <ImageView
                android:layout_gravity="end|bottom"
                android:contentDescription="@null"
                android:layout_marginBottom="@dimen/_5sdp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_arrow_bottom"
        />
    </FrameLayout>
    
    0 讨论(0)
提交回复
热议问题