首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >怪异的旋转行为

怪异的旋转行为
EN

Stack Overflow用户
提问于 2013-11-26 15:07:17
回答 2查看 523关注 0票数 0

我使用了一个自定义适配器来填充我的旋转器。我已经重写了getDropDownView,从它返回下拉列表的每一行的视图。除了呈现的下拉列表之外,所有操作都很好,没有获得Spinner小部件的宽度。相反,它是这样的:

因此下拉列表缺少突出显示的宽度。我不知道为什么会这样。我想要它得到旋转器的全部宽度。

我的自定义适配器:

代码语言:javascript
复制
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;
        }
    }

代码,我在这里设置这个适配器:

代码语言:javascript
复制
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:中旋转项的声明

代码语言:javascript
复制
 <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:

代码语言:javascript
复制
<?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>

我怎样才能解决这个问题?

EN

回答 2

Stack Overflow用户

发布于 2013-11-26 15:15:44

试试这个:

代码语言:javascript
复制
convertView=inflater.inflate(android.R.layout.simple_list_item_1, parent,false);
票数 0
EN

Stack Overflow用户

发布于 2013-11-26 22:09:00

这个问题的起因是“android:background=”@drawable/btn_下拉列表“这部分代码。当您删除它时,您将看到旋转器按其应有的方式工作,这意味着btn_dropdown drawable与您的旋转器的宽度相冲突,并且最终它是弹出窗口,这就是您看到这种奇怪行为的原因。我建议您调整您的btn_dropdown绘图,这样它就不会与spinner的默认行为冲突。看一看这些帖子:

我还没试过,但我想你会找到你想要的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20220758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档