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 13:55

You can try using dps.

@Override   
public View getDropDownView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    TextView textview = (TextView)inflater.inflate(android.R.layout.simple_spinner_item, null);

    textview.setText(alOpcion.get(position).getOpcionName());

    DisplayMetrics metrics = parent.getResources().getDisplayMetrics();
    float dp = 40f;
    float fpixels = metrics.density * dp;
    int pixels = (int) (fpixels + 0.5f);

    textview.setHeight(pixels);

    return textview;
}
查看更多
神经病院院长
3楼-- · 2020-05-26 13:55

add this in your adapter : convertView.setMinimumHeight(60); which works for me.

查看更多
老娘就宠你
4楼-- · 2020-05-26 13:57

Maybe it can help..

ArrayAdapter<String> yourSpinnerAdapter = new ArrayAdapter<String>(this,
                R.layout.spinner_item, yourItem) {

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            convertView = super.getDropDownView(position, convertView,
                    parent);

            convertView.setVisibility(View.VISIBLE);
            ViewGroup.LayoutParams p = convertView.getLayoutParams();
            p.height = 100; // set the height
            convertView.setLayoutParams(p);

            return convertView;
        }
    };
查看更多
做个烂人
5楼-- · 2020-05-26 14:01

In loginrowspinner.xml, add android:minHeight="48dp" to TextView element.

 <TextView 
     ...
     android:id="@+id/textViewRowFacility"
     android:minHeight="48dp" />
查看更多
何必那么认真
6楼-- · 2020-05-26 14:01

//This might also help:

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dataForAdapter) {


        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            v.setMinimumHeight((int) (40*cx.getResources().getDisplayMetrics().density));
            v.setBackgroundColor(Color.rgb(222, 222, 222));


            return v;
        }

    };
查看更多
Ridiculous、
7楼-- · 2020-05-26 14:01

As people say, best way is to add

    android:minHeight="48dp"

to .xml. But if you want to do it programatically use:

    mspinner.setMinimumHeight(48);

being mspinner the name of your spinner and 48 the new height of the option.

查看更多
登录 后发表回答