Remove Spacing or Padding in android Spinner

2019-01-18 17:53发布

问题:

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 spacing or padding. So, I would like to remove it to align text to left. Please help how to remove it.

回答1:

In your SpinnerAdapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        view.setPadding(0, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());
        return view;
}


回答2:

Try below code to do so. Change style attribute in your spinner list item layout to textViewStyle as below

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/textViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="false"
    android:textAlignment="inherit"
    android:textAppearance="?android:attr/textAppearanceMedium" />


回答3:

If you are using custom adapter, then try setting padding in getView() :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    view.setPadding(0, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());
    return view;
}


回答4:

You can also remove the padding of spinner in xml

Only need to set padding to 0dp.

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:padding="0dp"     
        android:id="@+id/my_spinner">


回答5:

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.



回答6:

The layout for TextView that is displayed in your Spinner comes from your SpinnerAdapter, which provides 2 methods:

  • getView(position, convertView, parent): returns View for collapsed state, you can override this to return different layouts for different selected item
  • getDropdownView(position, convertView, parent): return View for each dropdown item in expanded state (after you click Spinner to open popup/dialog for selection)

In your case you should override your SpinnerAdapter.getView(position, convertView, parent) and inflate the layout with padding/spacing of your choice.

For example:

spinner.setAdapter(new MyAdapter());

MyAdapter.java

class MyAdapter implements SpinnerAdapter {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.spinner_item, parent, false);
        }
        // bind your data here
        return convertView;
    }
    // other implementation
}

res/layout/spinner_item.xml

<!-- set padding margin whatever here -->
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

You can also conveniently use one of the framework provided SpinnerAdapter indirect subclasses (ArrayAdapter<T>, BaseAdapter, CursorAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, ThemedSpinnerAdapter), just make sure to supply the correct layout that would be used by getView() method.



回答7:

If yo don't want to mess in Java Code and have it in styles

Search for this file (Cmd+Shift+O) abc_spinner_textfield_background_material.xml

and copy to your project. Remove all inset values from root element and refer the file as background of your spinner.

In my case it looks like this (drawable/spinner_background.xml)

<inset xmlns:android="http://schemas.android.com/apk/res/android">
    <selector>
        <item
            android:state_checked="false"
            android:state_pressed="false">
            <layer-list>
                <item android:drawable="@drawable/abc_textfield_default_mtrl_alpha" />
                <item android:drawable="@drawable/abc_spinner_mtrl_am_alpha" />
            </layer-list>
        </item>
        <item>
            <layer-list>
                <item android:drawable="@drawable/abc_textfield_activated_mtrl_alpha" />
                <item android:drawable="@drawable/abc_spinner_mtrl_am_alpha" />
            </layer-list>
        </item>
    </selector>
</inset>

and in my styles.xml I use it this way

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


回答8:

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
    }
}


回答9:

You can customize your own Spinner via Nine patch and specify padding



回答10:

I just hit this issue as well. I was able to solve it with adjusting margin actually instead of padding, so setting android:marginStart="0dp" to the spinner item.



回答11:

This Work for me

Spinner spn = (Spinner) findViewById(R.id.spn);

spn.setPadding(0, spn.getPaddingTop(), spn.getPaddingRight(), spn.getPaddingBottom());