Change Spinner DropDown width

前端 未结 2 1626
别跟我提以往
别跟我提以往 2021-02-05 04:40

I need resize this part size to full display. How can i do this?

\"Example

My adapter:



        
相关标签:
2条回答
  • 2021-02-05 04:49

    What you need to do is use a custom adapter for the drop-down instead of the default one, where you set the "minimumWidth" of each 'row' to whatever you want:

    private class myCustomAdapter extends ArrayAdapter{
        private List<String> _navigations;
        private int _resource;
        private int _textViewResourceId;
    
        public myCustomAdapter (Context context, int resource, int textViewResourceId, List<String> objects) {
            super(context, resource, textViewResourceId, objects);
            _navigations = objects;
            _resource = resrouce;
            _textViewResourceId = textViewResourceId;
        }
    
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent){
            View row;
            LayoutInflater inflater=getLayoutInflater();            
            row = inflater.inflate(_resource, null);
            TextView _textView = row.findViewById(_textViewResourceId);
            _textView.setText(_navigations.get(position));
    
    
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int _width = size.x;
    
            row.setMinimumWidth = _width;
            return row;
        }
    }
    

    You can of course play with "minimumWidth" to be whatever else you want; In this example it's just set to match the screen width (though a smarter approach would be to measure your app's container frame and match it to that).

    Then to set the adapter:

    myCustomAdapter adapter = new myCustomAdapter(getBaseContext(), R.layout.custom_spinner_title_bar,android.R.id.text1, navigations);   
    
    0 讨论(0)
  • 2021-02-05 05:07

    Add Attribute in xml file in Spinner tag

    android:dropDownWidth="150dp"
    
    0 讨论(0)
提交回复
热议问题