Remove Spacing or Padding in android Spinner

2019-01-18 17:55发布

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.

enter image description here

11条回答
Anthone
2楼-- · 2019-01-18 18:30

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" />
查看更多
可以哭但决不认输i
3楼-- · 2019-01-18 18:30

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">
查看更多
Evening l夕情丶
4楼-- · 2019-01-18 18:30

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.

查看更多
看我几分像从前
5楼-- · 2019-01-18 18:33

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
    }
}
查看更多
Anthone
6楼-- · 2019-01-18 18:34

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;
}
查看更多
手持菜刀,她持情操
7楼-- · 2019-01-18 18:35

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

查看更多
登录 后发表回答