奇怪的Spinner行为

时间:2013-11-26 15:07:17

标签: android android-layout android-spinner

我使用自定义适配器来填充我的Spinner。我已经覆盖了getDropDownView,我从中返回下拉列表的每一行的视图。除了渲染的下拉列表没有获得Spinner小部件的宽度之外,一切正常。而是它像这样: enter image description here

因此下拉列表缺少突出显示的宽度。我不知道为什么会这样。我希望它能获得微调器的整个宽度。

我的自定义适配器:

class CategorySpinnerAdapter extends ArrayAdapter{

        private Activity context;
        ArrayList<Category> categoryList;
        public CategorySpinnerAdapter(Activity context,int resourceID,ArrayList<Category> categoryList)
        {
            super(context,resourceID,categoryList);

            this.context=context;
            this.categoryList=categoryList;
        }

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub

            if(convertView==null)
            {
                LayoutInflater inflater=context.getLayoutInflater();
                convertView=inflater.inflate(R.layout.category_spinner_row, parent,false);
            }

            Category currentCategory=categoryList.get(position);

            TextView categoryText=(TextView) convertView.findViewById(R.id.spinnerText);
            categoryText.setText(currentCategory.getCategoryName());

            return convertView;
        }
    }

代码,我在哪里设置此适配器:

Spinner categorySpinner=(Spinner) getActivity().findViewById(R.id.categorySpinner);
        ArrayList<Category> categoryList=populateCategoryList();

        CategorySpinnerAdapter categorySpinnerAdapter=new CategorySpinnerAdapter(getActivity(), android.R.layout.simple_spinner_item,categoryList);

        categorySpinner.setAdapter(categorySpinnerAdapter);

        categorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
                    int position, long id) {
                // TODO Auto-generated method stub

                    ArrayList<Reward> modifiedList=new ArrayList<Reward>();
                    //test case: category OK
                        int categoryID=position+1;
                        for(int i=0;i<rewardList.size();i++)
                        {
                            if(rewardList.get(i).getCategoryID()==categoryID)
                            {
                                modifiedList.add(rewardList.get(i));
                            }
                        }

                        adapter.changeDataSet(modifiedList);

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                //get default ELECTRONICS category 1 data to populate the list
                ArrayList<Reward> defaultCategorizedList=new ArrayList<Reward>();
                //test case: category OK

                    for(int i=0;i<rewardList.size();i++)
                    {
                        if(rewardList.get(i).getCategoryID()==1)
                        {
                            defaultCategorizedList.add(rewardList.get(i));
                        }
                    }

            }
        });

主xml中的Spinner项目声明:

 <Spinner
        android:id="@+id/categorySpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_below="@+id/customerRewardPointsTextView"
        android:background="@drawable/btn_dropdown"
        android:spinnerMode="dropdown" />
下拉项目的

布局,category_spinner_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:background="@drawable/category_spinner_background" >

    <TextView
        android:id="@+id/spinnerText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ellipsize="marquee"
        android:gravity="center"
        android:singleLine="true" />

</RelativeLayout>

如何解决此问题?

2 个答案:

答案 0 :(得分:0)

试试这个:

convertView=inflater.inflate(android.R.layout.simple_list_item_1, parent,false);

答案 1 :(得分:0)

问题的出现是因为“android:background =”@ drawable / btn_dropdown“”这部分代码。当你删除它时你将看到微调器正在按预期工作,这意味着btn_dropdown drawable与你的微调器的宽度相冲突,最终它的弹出窗口就是你看到这种奇怪行为的原因。我建议你调整你的btn_dropdown drawable,这样它就不会与spinner的默认行为发生冲突。看看这些帖子:

我没有尝试过这些,但我认为你会找到你需要的东西。

相关问题