Change Spinner DropDown width

房东的猫 提交于 2019-12-03 11:02:18

问题


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

My adapter:

String[] navigations = getResources().getStringArray(R.array.actionBar);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getBaseContext(), R.layout.custom_spinner_title_bar,
                android.R.id.text1, navigations);
        adapter.setDropDownViewResource(R.layout.custom_spinner_title_bar);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        actionBar.setListNavigationCallbacks(adapter, navigationListener);

custom_spinner_title_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="fill_horizontal"
    android:orientation="vertical" >

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dip"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

</RelativeLayout>

回答1:


Add Attribute in xml file in Spinner tag

android:dropDownWidth="150dp"



回答2:


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


来源:https://stackoverflow.com/questions/18235711/change-spinner-dropdown-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!