How to set dropdown arrow in spinner?

前端 未结 10 665
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 14:40

I tried to set spinner with drop down arrow but i couldn\'t fix it can anyone help me with this? I have attached the source code.

my class file:

             


        
相关标签:
10条回答
  • 2020-11-27 14:52

    Basically one needs to create a custom background for a spinner. It should be something like this:

    spinner_background.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item>
    
            <layer-list>
    
                <item>
                    <color
                        android:color="@android:color/white"/>
                </item>
    
                <item>
                    <bitmap
                        android:gravity="center_vertical|right"
                        android:src="@drawable/ic_arrow_drop_down_black_24dp"/>
                </item>
    
            </layer-list>
    
        </item>
    
    </selector>
    

    Then create a custom style for your spinner, where you specify the above selector as background:

    <style name="Widget.App.Spinner" parent="@style/Widget.AppCompat.Spinner">
        <item name="overlapAnchor">true</item>
        <item name="android:background">@drawable/spinner_background</item>
    </style>
    

    And finally in your app theme you should override two attributes if you want it to be applied all across your app:

    <item name="spinnerStyle">@style/Widget.App.Spinner</item>
    <item name="android:spinnerStyle">@style/Widget.App.Spinner</item>
    

    And that's pretty much it.

    0 讨论(0)
  • 2020-11-27 14:59
                       <Spinner
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginRight="16dp"
                        android:paddingLeft="10dp"
                        android:spinnerMode="dropdown" />
    
    0 讨论(0)
  • 2020-11-27 14:59

    Set the Dropdown arrow image to spinner like this :

    <Spinner
        android:id="@+id/Exam_Course"
        android:layout_width="320dp"
        android:background="@drawable/spinner_bg"
        android:layout_height="wrap_content"/>
    

    Here android:background="@drawable/spinner_bg" the spinner_bg is the Dropdown arrow image.

    0 讨论(0)
  • 2020-11-27 15:02

    copy and paste this xml instead of your xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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"
        android:background="@drawable/back1"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="wrap_content"
            android:layout_height="55dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="20dp" 
            android:background="@drawable/red">
    
            <Spinner
                android:id="@+id/spinner1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:dropDownWidth="fill_parent" 
                android:background="@android:drawable/btn_dropdown"
             />
    
        </LinearLayout>
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="55dp"
            android:layout_alignLeft="@+id/linearLayout1"
            android:layout_alignRight="@+id/linearLayout1"
            android:layout_below="@+id/linearLayout1"
            android:layout_marginTop="25dp"
            android:background="@drawable/red"
            android:ems="10"
            android:hint="enter card number" >
    
            <requestFocus />
        </EditText>
    
        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="55dp"
            android:layout_alignLeft="@+id/editText1"
            android:layout_alignRight="@+id/editText1"
            android:layout_below="@+id/editText1"
            android:layout_marginTop="33dp"
            android:orientation="horizontal" 
            android:background="@drawable/red">
    
            <Spinner
                android:id="@+id/spinner3"
                android:layout_width="72dp"
                android:layout_height="wrap_content"
                android:background="@android:drawable/btn_dropdown"
                 />
    
            <Spinner
                android:id="@+id/spinner2"
                android:layout_width="72dp"
                android:layout_height="wrap_content" 
                android:background="@android:drawable/btn_dropdown"
                />
    
            <EditText
                android:id="@+id/editText2"
                android:layout_width="22dp"
                android:layout_height="match_parent"
                android:layout_weight="0.18"
                android:ems="10"
                android:hint="enter cvv" />
    
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="wrap_content"
            android:layout_height="55dp"
            android:layout_alignParentLeft="true"
            android:layout_alignRight="@+id/linearLayout2"
            android:layout_below="@+id/linearLayout2"
            android:layout_marginTop="26dp"
            android:orientation="vertical"
            android:background="@drawable/red" >
        </LinearLayout>
    
        <Spinner
            android:id="@+id/spinner4"
            android:layout_width="15dp"
            android:layout_height="18dp"
            android:layout_alignBottom="@+id/linearLayout3"
            android:layout_alignLeft="@+id/linearLayout3"
            android:layout_alignRight="@+id/linearLayout3"
            android:layout_alignTop="@+id/linearLayout3"
            android:background="@android:drawable/btn_dropdown"
          />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/linearLayout3"
            android:layout_marginTop="18dp"
            android:text="Add Amount" 
            android:background="@drawable/buttonsty"/>
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-27 15:04

    Copy and paste this xml to show as a Dropdown and change your Dropdown color

     <?xml version="1.0" encoding="UTF-8"?><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"
    android:background="@drawable/back1"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp" 
        android:background="@drawable/red">
    
        <Spinner android:id="@+id/spinner1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:dropDownWidth="fill_parent" 
            android:popupBackground="@drawable/textbox"
            android:spinnerMode="dropdown"
            android:background="@drawable/drop_down_large"
    
         />
    
    </LinearLayout>
    
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:layout_alignLeft="@+id/linearLayout1"
        android:layout_alignRight="@+id/linearLayout1"
        android:layout_below="@+id/linearLayout1"
        android:layout_marginTop="25dp"
        android:background="@drawable/red"
        android:ems="10"
        android:hint="enter card number" >
    
        <requestFocus />
    </EditText>
    
    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="33dp"
        android:orientation="horizontal" 
        android:background="@drawable/red">
    
        <Spinner
            android:id="@+id/spinner3"
            android:layout_width="72dp"
            android:layout_height="wrap_content"
             android:popupBackground="@drawable/textbox"
            android:spinnerMode="dropdown"
            android:background="@drawable/drop_down_large"
             />
    
        <Spinner
            android:id="@+id/spinner2"
            android:layout_width="72dp"
            android:layout_height="wrap_content" 
            android:popupBackground="@drawable/textbox"
            android:spinnerMode="dropdown"
            android:background="@drawable/drop_down_large"
            />
    
        <EditText
            android:id="@+id/editText2"
            android:layout_width="22dp"
            android:layout_height="match_parent"
            android:layout_weight="0.18"
            android:ems="10"
            android:hint="enter cvv" />
    
    </LinearLayout>
    
    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/linearLayout2"
        android:layout_below="@+id/linearLayout2"
        android:layout_marginTop="26dp"
        android:orientation="vertical"
        android:background="@drawable/red" >
    </LinearLayout>
    
    <Spinner
        android:id="@+id/spinner4"
        android:layout_width="15dp"
        android:layout_height="18dp"
        android:layout_alignBottom="@+id/linearLayout3"
        android:layout_alignLeft="@+id/linearLayout3"
        android:layout_alignRight="@+id/linearLayout3"
        android:layout_alignTop="@+id/linearLayout3"
        android:popupBackground="@drawable/textbox"
            android:spinnerMode="dropdown"
            android:background="@drawable/drop_down_large"/>
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/linearLayout3"
        android:layout_marginTop="18dp"
        android:text="Add Amount"
        android:background="@drawable/buttonsty"/>
    

    0 讨论(0)
  • 2020-11-27 15:10

    Attach a Spinner Style using Java Code:

    First, you need to a layout file such as below:

    <?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="none"
    android:minHeight="?android:attr/listPreferredItemHeight" />
    

    Let us name it spinner_item.xml and place it inside res/layouts folder.

    Next, Create a String ArrayList and put all the Spinner options inside it:

    ArrayList<String> spinnerArray = new ArrayList<String>();
    spinnerArray.add("Item No. 1");
    spinnerArray.add("Item No. 2");
    spinnerArray.add("Item No. 3");
    spinnerArray.add("Item No. 4");
    

    Finally, create the Spinner object and attach the style layout to it.

    Spinner spinner = new Spinner(getActivity());
    spinner.setTag("some_id");
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);                  spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
    spinner.setAdapter(spinnerArrayAdapter);
    

    Note the Spinner(getActivity()) in the above line will be changed to Spinner(this) if you are writing this from inside Activity rather than from inside a fragment.

    Thats all!


    Attach a Spinner Style inside Android Layout File:

    First, create a xml file the defines the style attribute (gradient_spinner.xml)

    <?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:type="linear" />
    
                <stroke android:width="1dp" android:color="@color/colorBackground" />
    
                <corners android:radius="2dp" />
    
                <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
            </shape></item>
            <item android:right="5dp">
                <bitmap android:gravity="center_horizontal|right" android:src="@drawable/expand_icon">
                <padding android:right="2dp" />
            </bitmap>
            </item>
        </layer-list></item>
    
    </selector>
    

    Next, inside the style.xml file specify the style and call the gradient_spinner as background

    <style name="spinner_style">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@drawable/gradient_spinner</item>
    <item name="android:layout_margin">1dp</item>
    <item name="android:paddingLeft">5dp</item>
    <item name="android:paddingRight">5dp</item>
    <item name="android:paddingTop">5dp</item>
    <item name="android:paddingBottom">5dp</item>
    </style>
    

    Finally, attach the above style to the Spinner:

    <Spinner
       android:id="@+id/agent_id_spinner"
       android:layout_width="match_parent"
       android:layout_height="40dp"
       android:textSize="@dimen/title_text_view"
       style="@style/spinner_style"  />
    

    Thats it!

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