怪异的行为微调 [英] Weird Spinner behaviour

查看:212
本文介绍了怪异的行为微调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个自定义适配器来填充我的微调。我已经重写getDropDownView我从中返回下拉列表中的每一行的看法。除了渲染的是没有得到的微调控件的宽度的下拉列表中的一切工作正常。相反,它变得像这样:

I have used a custom adapter for populating my Spinner. I have overriden getDropDownView from which I return the view of each row of the dropdown list. Everything works fine except the dropdown list rendered is not getting the width of the Spinner widget. Rather it gets Like this:

所以下拉列表中缺少突出的宽度。我不知道为什么会这样。我想让它变得微调的整个宽度。

So the dropdown list is missing the highlighted width. I dont know why this is happening. I want it to get the full width of the spinner.

我的自定义适配器:

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

code,我在哪里设置这个适配器:

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

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

How can I fix this issue ?

推荐答案

这个问题开始,因为机器人:背景=@绘制/ btn_dropdown,code的这一部分。当你删除此,您将看到微调工作,因为它应该是,这意味着btn_dropdown绘制与您微调的宽度相冲突,并最终它的,这就是为什么你看到了这个怪异的行为弹出窗口。我会建议您调整btn_dropdown绘制,使其不与微调的默认行为发生冲突。看看这些后:

The problem starts because of "android:background="@drawable/btn_dropdown"" this portion of code. When you remove this you will see the spinner is working as it is supposed to, that means btn_dropdown drawable is conflicting with your spinner's width and eventually it's popup window that's why you are seeing this weird behaviour. I would suggest you to adjust your btn_dropdown drawable so that it does not conflict with spinner's default behaviour. Take a look at these post:

我没有尝试过的,但我认为你会发现你所需要的。

I haven't tried those but i think you will find what you need.

这篇关于怪异的行为微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆