更改Spinner DropDown宽度

时间:2013-08-14 15:20:30

标签: android spinner android-spinner titlebar android-titlebar

我需要调整此部件尺寸以完全显示。我怎么能这样做?

Example image

我的适配器:

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>

2 个答案:

答案 0 :(得分:80)

xml标记

中的Spinner文件中添加属性
android:dropDownWidth="150dp"

答案 1 :(得分:3)

您需要做的是使用自定义适配器作为下拉列表而不是默认适配器,您可以将每个'行'的“minimumWidth”设置为您想要的任何内容:

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

你当然可以玩“minimumWidth”来成为你想要的任何东西;在这个例子中,它只是设置为匹配屏幕宽度(尽管更聪明的方法是测量应用程序的容器框架并将其与之匹配)。

然后设置适配器:

myCustomAdapter adapter = new myCustomAdapter(getBaseContext(), R.layout.custom_spinner_title_bar,android.R.id.text1, navigations);