how to change the dropdown height in spinner

后端 未结 11 1622
粉色の甜心
粉色の甜心 2021-02-05 11:20

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



        
相关标签:
11条回答
  • 2021-02-05 11:50

    Just add this to your adapter.

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
    0 讨论(0)
  • 2021-02-05 11:53

    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;
            }
        };
    
    0 讨论(0)
  • 2021-02-05 11:53

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

     <TextView 
         ...
         android:id="@+id/textViewRowFacility"
         android:minHeight="48dp" />
    
    0 讨论(0)
  • 2021-02-05 11:57

    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 :)

    0 讨论(0)
  • 2021-02-05 11:57

    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;
    }
    
    0 讨论(0)
  • 2021-02-05 11:57

    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.

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