spinner 的使用

来源:互联网 发布:张逗张花家很富吗 知乎 编辑:程序博客网 时间:2024/05/20 01:36
   private void initTitleSpinner() {        final ArrayList<String> titleNameList = new ArrayList<String>();        titleNameList.add(SIMPLE_VERSION_FEEDBACK);        titleNameList.add(PROFESSIONAL_VERSION_FEEDBACK);        final ArrayAdapter<String> titleSpinnerAdapter = new ArrayAdapter<String>(this, R.layout.title_spinner_style, titleNameList) {            @Override            public View getDropDownView(int position, View convertView, ViewGroup parent) {                View view = (getLayoutInflater().inflate(R.layout.title_spinner_view, parent, false));                TextView item = (TextView) view.findViewById(R.id.title_spinner_item);                item.setText(getItem(position));                if (titleSpinner.getSelectedItemPosition() == position) {//                    view.setBackgroundColor(getResources().getColor(R.color.screenshot_spinner_selected));                    item.setTextColor(Color.RED);                }                return view;            }        };        titleSpinner.setAdapter(titleSpinnerAdapter);        titleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {                String titleName = null;                if (titleNameList.size() > arg2) {                    titleName = titleNameList.get(arg2);                }                if (titleName != null && titleName.equals(SIMPLE_VERSION_FEEDBACK)) {                    FeedbackOverallMgr.getInstance().unSetFeedbackProfessional();                } else if (titleName != null && titleName.equals(PROFESSIONAL_VERSION_FEEDBACK)) {                    FeedbackOverallMgr.getInstance().setFeedbackProfessional();                }                FeedbackOverallMgr.addUserTrack(FeedbackOverallMgr.UserTrackState.POINT_TITLE_SPINNER_CLICK);            }            public void onNothingSelected(AdapterView<?> arg0) {                // TODO Auto-generated method stub            }        });        if(FeedbackOverallMgr.getInstance().isFeedbackProfessional())        {            if(titleNameList.size() >= 2)            {                titleSpinner.setSelection(1);            }        }        else        {            if(titleNameList.size() >= 1)            {                titleSpinner.setSelection(0);            }        }    }

其中R.layout.title_spinner_style 文件是自定义spinner样式,该布局文件如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView android:id="@+id/title_spinner_item"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:textColor="#FFFFFF"        android:textSize="14dp"        android:background="@color/screenshot_blue"        android:paddingBottom="5dp"        android:paddingTop="5dp"/></LinearLayout>

其中R.layout.title_spinner_view文件是对spinner的下拉框样式的自定义,如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView android:id="@+id/title_spinner_item"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:textColor="#FFFFFF"        android:textSize="14dp"        android:background="@color/screenshot_blue"        android:paddingBottom="5dp"        android:paddingTop="5dp"/></LinearLayout>
0 0
原创粉丝点击