27

我需要将此部分尺寸调整为完全显示。我怎样才能做到这一点?

示例图片

我的适配器:

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>
4

2 回答 2

94

在标签中的xml文件中添加属性Spinner

android:dropDownWidth="150dp"
于 2014-02-11T13:39:19.847 回答
5

您需要做的是为下拉菜单使用自定义适配器而不是默认适配器,您可以在其中将每个“行”的“最小宽度”设置为您想要的任何内容:

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);   
于 2013-11-14T10:03:24.117 回答