【问题标题】:Change Spinner DropDown width更改微调器下拉宽度
【发布时间】:2013-08-16 15:22:14
【问题描述】:

我需要调整这个部件的大小以完全显示。我该怎么做?

我的适配器:

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>

【问题讨论】:

  • 尝试让您的微调器成为其水平父布局中的唯一视图。我的意思是尝试删除它旁边的红色按钮,看看它是否有帮助

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


【解决方案1】:

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

android:dropDownWidth="150dp"

【讨论】:

  • 适用于默认 spinnermode,但如何使其与 android:spinnerMode="dialog" 一起使用?
  • 虽然我们在这里,但也许有人知道如何设置下拉高度?也许可以限制我想显示的项目数量。
【解决方案2】:

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

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

【讨论】:

  • 谢谢你,但现在我不能接受你的回答,因为我拒绝实施这个
  • 理论上您可以将微调器本身和 usr setDropDownWidth 设为目标,但这仍然需要自定义适配器,并且仅适用于 Jellybean 及更高版本。
猜你喜欢
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多