更改Spinner下拉宽度

29

我需要将这一部分大小调整为全屏显示。我该如何做?

示例图片

我的适配器:

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

自定义旋转进度条标题栏.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>

尝试让您的旋转器成为其水平父布局中唯一的视图。我的意思是尝试将旁边的红色按钮移除,看看是否有所帮助。 - arianoo
2个回答

99
Spinner标签中添加xml文件属性
android:dropDownWidth="150dp"

5
默认的Spinner模式可用,但如何使其与android:spinnerMode="dialog"一起工作? - live-love
顺便问一下,也许有人知道如何设置下拉菜单的高度吗?我想可能可以设置显示的项目数量限制。 - matthias_b_nz

6
你需要做的是使用自定义适配器而不是默认适配器来处理下拉菜单,你可以设置每一行的“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);   

1
谢谢您的回答,但我现在不能接受您的答案,因为我拒绝实现它。 - WOLVERINE
3
理论上,您可以针对下拉列表控件本身使用setDropDownWidth方法来调整宽度,但这仍需要自定义适配器,并且只能在Android 4.1及以上版本中使用。 - Elad Avron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接