Remove Spacing or Padding in android Spinner

后端 未结 14 1770
旧时难觅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:40

    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;
    }
    
    0 讨论(0)
  • 2020-12-15 16:40

    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.

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

    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" />
    
    0 讨论(0)
  • 2020-12-15 16:45

    This Work for me

    Spinner spn = (Spinner) findViewById(R.id.spn);
    
    spn.setPadding(0, spn.getPaddingTop(), spn.getPaddingRight(), spn.getPaddingBottom());
    
    0 讨论(0)
  • 2020-12-15 16:46
    1. Create a text-view layout view_spinner_text_view.xml:

      <TextView xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
      
    2. Create the adapter by providing the above layout:

          ArrayAdapter.createFromResource(
              context,
              R.array.my_values,
              R.layout.view_spinner_text_view // <-- this is for the spinner itself
          ).also { adapter ->
              adapter.setDropDownViewResource(
                  android.R.layout.simple_spinner_dropdown_item // <-- this is for the dropped-down items (you can customize this one as well)
              )
              spinner.adapter = adapter
          }
      
    0 讨论(0)
  • 2020-12-15 16:47

    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.

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