how to change the dropdown height in spinner

2020-05-26 13:53发布

i tried a lot to change the dropdown item height of spinner.. but i couldn't get a good solution.. plz help me guys..

here is a code loginactivityview.xml

       <Spinner
         android:id="@+id/spinnerFacility"
         android:layout_width="400dip"
         android:layout_height="50dip"
         android:layout_alignLeft="@+id/lpassword"
         android:layout_below="@+id/lpassword"
         android:layout_marginTop="32dip"            
         android:background="@drawable/location" 
         android:paddingLeft="10dip"               
         android:dropDownWidth="@style/dropDown"  
         android:minHeight="40dip"     
         android:typeface="monospace" />

loginrowspinner.xml

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

  <TextView
    android:id="@+id/textViewRowFacility"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="left"
    android:paddingBottom="5dip"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:paddingTop="5dip"
    android:text="Facility"
    android:textColor="#000000"
    android:textSize="20dip" >
</TextView>

enter image description here

how to change the height of drop down any idea..

11条回答
够拽才男人
2楼-- · 2020-05-26 14:02

Have you tried something like android:layout_height="30dp" instead of fill_parent for either of those elements in loginrowspinner.xml?

查看更多
疯言疯语
3楼-- · 2020-05-26 14:08

Override method in ArrayAdapter class

@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View view = super.getDropDownView(position, convertView, parent);
    view.getLayoutParams().height = 50;
    return view;
}
查看更多
看我几分像从前
4楼-- · 2020-05-26 14:12

Just add this to your adapter.

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
查看更多
叼着烟拽天下
5楼-- · 2020-05-26 14:13

Got stuck in same problem.Found following solution to be working for me:

 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
        setSpinnerDropDownHeight()
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mBinding?.included?.spinn?.getViewTreeObserver()?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
                @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
                override fun onGlobalLayout() {
                    mBinding?.included?.spinn?.dropDownVerticalOffset = mBinding?.included?.spinn?.getDropDownVerticalOffset()!! + mBinding?.included?.spinn?.getHeight()!!
                    mBinding?.included?.spinn?.viewTreeObserver!!.removeOnGlobalLayoutListener(this)
                }
            })

            mBinding?.included?.spinn?.dropDownVerticalOffset = 10
        }
    }

Hope it helps someone!!!!

Thanks

查看更多
兄弟一词,经得起流年.
6楼-- · 2020-05-26 14:15

Create your own textview in layout folder like this which will be populated in dropdown popup

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="18sp"
android:textAlignment="inherit" />

Note I have provided

android:layout_height="60dp"

and

android:paddingLeft="20dp"

and use this for your spinner, like this

Spinner dropdown = (Spinner)findViewById(R.id.sosMode);
String[] items = new String[]{"Date", "Travelling alone"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_dropdown_list, items);
dropdown.setAdapter(adapter);

this works for me :)

查看更多
登录 后发表回答