Change spinner background color but keep arrow

前端 未结 11 1342
予麋鹿
予麋鹿 2020-12-13 01:41

I\'ve read several thing about it but I can\'t find what I need. I want to keep the grey arrow but I want to remove the horizontal bar from the default style and have a whit

相关标签:
11条回答
  • 2020-12-13 02:00

    Use this:

    yourspinner.setOnItemSelectedListener(this);
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        ((TextView) yourspinner.getSelectedView()).setBackgroundColor(getResources()
                .getColor(R.color.your_color));
    }
    

    and your class should implement OnItemSelectedListener.

    0 讨论(0)
  • 2020-12-13 02:02

           android:layout_alignParentRight="true"
            android:layout_width="@dimen/spinner_width"
            android:layout_height="wrap_content"
            android:id="@+id/spnLocation"
            android:entries="@array/labelFamily"
            android:layout_centerVertical="true"
    
            android:backgroundTint="@color/color_gray"
    

    this work for me

    0 讨论(0)
  • 2020-12-13 02:04

    below layout will create a background in spinner with desire color drop down arrow

      <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".6"
            android:background="@drawable/edit_text_rounded_shape"
            android:gravity="center|center_vertical">
        <Spinner
            android:id="@+id/spinerComanyName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:layout_weight=".6"
            android:layout_gravity="center"
    
            android:entries="@array/spinner_item"
            android:spinnerMode="dropdown"
            android:theme="@style/Spinner"
    
            />
       </LinearLayout>
    
       <style name="Spinner">
        <!-- Used for the bottom line when not selected / focused -->
        <item name="colorControlNormal">@color/black</item>
        <item name="colorControlActivated">@color/colorPrimary</item>
        <!-- colorControlActivated & colorControlHighlight use the colorAccent          color by default -->
       </style>
    

    edit_text_rounded_shape which provide background color and rounded corner

       <?xml version="1.0" encoding="utf-8"?>
    
       <shape xmlns:android="http://schemas.android.com/apk/res/android"
             android:shape="rectangle" android:padding="10dp">
             <solid android:color="@color/white"/>
             <stroke android:color="@color/grey"/>
         <corners
         android:bottomRightRadius="15dp"
         android:bottomLeftRadius="15dp"
         android:topLeftRadius="15dp"
         android:topRightRadius="15dp"/>
      </shape>
    
    0 讨论(0)
  • 2020-12-13 02:05

    For the record, I found an easy solution : Wrap your spinner in a relative layout and add an image :

     <RelativeLayout 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:background="@drawable/borderbottom_white"<!-- white background with bottom border -->
         android:layout_marginTop="15dp"  >
            <Spinner
            android:id="@+id/postfield_category"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:background="@null"
            android:minHeight="0dp" />
            <ImageView 
             android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:src="@drawable/arrowspinner" />
        </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-13 02:05

    Always while working with spinners, buttons and EditTexts and also needing to insert a drawable, I often wrap the element (spinner, EditText etc.) in a FrameLayout. Check an example:

    <FrameLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content">
    
                                    <Spinner
                                        android:id="@+id/spinner_component"                                
                                        android:layout_width="match_parent"
                                        android:layout_height="45dp"
                                        android:background="@drawable/your_rectangle_style"
                                        android:textColor="@color/your_text_color" />
    
                                    <ImageView
                                        android:layout_width="11dp"
                                        android:layout_height="9dp"
                                        android:src="@drawable/arrow_spinner"
                                        android:layout_gravity="right|center_vertical"
                                        android:layout_marginRight="7dp" />
                                </FrameLayout>
    

    Another important tip is that FrameLayout allows you to set OnClick functions on the drawable, for instance.

    0 讨论(0)
  • 2020-12-13 02:07

    Hi instead of wrapping Spinner component around Parent Layouts like LinearLayout, RelativeLayout etc which increases layout parsing simply create a drawable called spinner_bg.xml under drawable folder.

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
            <bitmap
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:src="@drawable/icn_dropdown_arw" />
        </item>
    </layer-list>
    

    Set spinner_bg as the background of your spinner and it works like charm:

    <Spinner  
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:padding="5dp"
         android:background="@drawable/spinner_bg" />
    
    0 讨论(0)
提交回复
热议问题