Remove Spacing or Padding in android Spinner

后端 未结 14 1768
旧时难觅i
旧时难觅i 2020-12-15 16:01

I\'m using android spinner and edittext from support lib v21. I would like to align text to left the same like edittext as shown in figure. But, deafult spinner include spac

相关标签:
14条回答
  • 2020-12-15 16:29

    This array adapter removes left padding in Android Spinner.

    Kotlin version:

    open class NoPaddingArrayAdapter<T>(context: Context, layoutId: Int, items: List<T>?) : ArrayAdapter<T>(context, layoutId, items) {
    
        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
            val view = super.getView(position, convertView, parent)
            view.setPadding(0,view.paddingTop,view.paddingRight,view.paddingBottom)
            return view
        }
    }
    
    0 讨论(0)
  • 2020-12-15 16:32

    Try this,

    1. Create custom layout(as you need) for spinner row.

    2. inflate the layout when you set the data adapter to the spinner.

    0 讨论(0)
  • 2020-12-15 16:32

    in your onViewCreated()

    spinner.post { spinner.setPadding(0,0,0,0) } 
    
    0 讨论(0)
  • 2020-12-15 16:33

    Maybe this is the simplest way and also it just work. To decrease left padding of Spinner, you can use set padding left to minus so it will move to the left. For example android:layout_marginLeft="-8dp" This is not a best practice, only a simple hack. Here is a code sample:

    <Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:layout_marginLeft="-8dp"
    android:entries="@array/arr_tipe_kendaraan"
    android:gravity="left"
    android:paddingTop="14dp"
    android:paddingBottom="8dp"
    android:prompt="@string/action_sign_in"
    android:spinnerMode="dialog" />
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-15 16:33

    This Kotlin code is a little bit more succinct than the getView override in another answer. It makes it a bit more clear in fewer lines that it is mostly just doing what super does, except that it also overrides setPaddingfor the left value.

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        return super.getView(position, convertView, parent).apply {
            setPadding(0, paddingTop, paddingRight, paddingBottom)
        }
    }
    

    Here is my full fragment code that uses a data binding object for the complete spinner array adapter override, which also does a selected item and an alternating background color in getDropDownView:

    binding.editSpinner.apply {
        this.context ?: return@apply
        this.adapter = object : ArrayAdapter<Any>(
            requireContext(),
            android.R.layout.simple_spinner_dropdown_item,
            SpinnerTypeValues.values().map { it.text }
        ) {
            override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
                return super.getDropDownView(position, convertView, parent).also {
                    when {
                        position == this@apply.selectedItemPosition -> it.setBackgroundColor( Color.rgb(245, 212, 119) )
                        position % 2 == 0 -> it.setBackgroundColor(Color.rgb(240, 240, 240))
                        else -> it.setBackgroundColor(Color.rgb(214, 235, 189))
                    }
                }
            }
            override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
                return super.getView(position, convertView, parent).apply {
                    setPadding(0, paddingTop, paddingRight, paddingBottom)
                }
            }
        }
        this.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {}
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                viewModel.saveSpinnerSelection(position)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 16:35

    Replace android.R.layout.simple_spinner_item layout from code by new spinner_item layout

    Code for spinner_item:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/primaryText"
        android:textColor="@color/black"
        android:textStyle="bold"/>
    

    This will remove an extra margin and you can style it according to your requirement.Thanks.

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