5

我的布局中有一个微调器并使用android:prompt="@string/year".

默认提示如下所示:

默认提示看起来像

现在我的问题是:

  1. 如何将默认图标更改为另一个图像?
  2. 如何让“年份”文本以红色显示在中心?
  3. 如何将背景颜色更改为黄色(存在“年份”文本和图标的背景)?

我的自定义适配器类

private class MyCustomSpinnerAdapter extends ArrayAdapter<ArrayList> {
        private ArrayList<String> objectsList = new ArrayList<String>();

        @SuppressWarnings("unchecked")
        public MyCustomSpinnerAdapter(Context context, int textViewResourceId,
                ArrayList objects) {
            super(context, textViewResourceId, objects);
            this.objectsList = objects;
        }

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView1(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView,
                ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View rowView = inflater.inflate(R.layout.spinner_dropdown, parent,
                    false);
            LinearLayout rowTitle = (LinearLayout) rowView
                    .findViewById(R.id.row);

            rowTitle.setBackgroundResource(R.drawable.spinner_row_focused);

            TextView textView = (TextView) rowView.findViewById(R.id.title);
            textView.setTypeface(typeFace);
            textView.setText(objectsList.get(position).toString().trim());
            return rowView;
        }

        public View getCustomView1(int position, View convertView,
                ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View rowView = inflater.inflate(R.layout.spinner_dropdown, parent,
                    false);

            TextView textView = (TextView) rowView.findViewById(R.id.title);
            textView.setText(objectsList.get(position).toString().trim());
            textView.setTypeface(typeFace);
            return rowView;
        }
    }
4

5 回答 5

3

这是您的解决方案:为标题(文件R.layout.spinner_head)创建简单的布局:

<ImageView
    android:id="@+id/spin_img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1.0" 
    android:padding="5dip"
    android:src="@android:drawable/btn_star"
    android:layout_alignParentLeft="true"/>

<TextView
    android:id="@+id/title"
    android:textColor="#f00"
    android:gravity="center"
    android:text="Year"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" 
    />

创建自定义微调器:

class MySpinner extends Spinner {

        public MySpinner(Context context) {
            super(context);
        }

        @Override
        public boolean performClick() {
            boolean handled =  false;//super.performClick();

            if (!handled) {

                handled = true;

                Context context = getContext();

                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                CharSequence mPrompt = getPrompt();
                if (mPrompt != null) {

                    builder.setCustomTitle(getLayoutInflater().inflate(R.layout.spinner_head, null));

                }

                builder.setSingleChoiceItems(new DropDownAdapter(getAdapter()),getSelectedItemPosition(), this).show();

            }

            return handled;
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            setSelection(which);
            dialog.dismiss();
        }

        class  DropDownAdapter implements ListAdapter, SpinnerAdapter {
            private SpinnerAdapter mAdapter;

            public  DropDownAdapter(SpinnerAdapter adapter) {
                this.mAdapter = adapter;
            }

            @Override
            public int getCount() {
                return mAdapter == null ? 0 : mAdapter.getCount();
            }

            @Override
            public Object getItem(int position) {
                return mAdapter == null ? null : mAdapter.getItem(position);
            }

            @Override
            public long getItemId(int position) {
                return mAdapter == null ? -1 : mAdapter.getItemId(position);
            }

            @Override
            public int getItemViewType(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                return getDropDownView(position, convertView, parent);
            }

            @Override
            public int getViewTypeCount() {
                return 1;
            }

            @Override
            public boolean hasStableIds() {
                return mAdapter != null && mAdapter.hasStableIds();
            }

            @Override
            public boolean isEmpty() {
                return getCount() == 0;
            }

            @Override
            public void registerDataSetObserver(DataSetObserver observer) {
                 if (mAdapter != null) {
                     mAdapter.registerDataSetObserver(observer);
                 }

            }

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {
                 if (mAdapter != null) {
                     mAdapter.unregisterDataSetObserver(observer);
                 }

            }

            @Override
            public View getDropDownView(int position, View convertView,
                    ViewGroup parent) {
                return mAdapter == null ? null : mAdapter.getDropDownView(position, convertView, parent);
            }

            @Override
            public boolean areAllItemsEnabled() {
                return true;
            }

            @Override
            public boolean isEnabled(int arg0) {
                return true;
            }


        }
    }

创建微调器的实例

MySpinner theSpin = new MySpinner(this);
            theSpin.setPrompt("Select Item");
            ArrayAdapter<String> theAdapter = 
                new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, new String[]{"item1","item2","item3","item4","item5","item6" });
            theSpin.setAdapter(theAdapter);

现在使用这个自定义微调器,它将显示自定义标题。您可以根据需要修改该标题。

于 2012-10-23T09:18:40.410 回答
1

我认为你最好准备像自定义 ListView 这样的自定义微调器并检查此链接http://android-er.blogspot.in/2010/12/custom-spinner-with-icon.html

于 2012-10-08T09:03:38.280 回答
1

这里只需更改名称微调器的设置提示并将名称作为字符串传递在那里,下面我给出的语法如下所示 -

spinner.setPrompt("Name of Your Header");
于 2012-10-27T06:46:02.217 回答
0

我查看了Spinner类代码,有DialogPopup一个内部私有类。此类封装AlertDialogListView显示在 Spinner 对话框中。它是私有的,所以我们不能扩展它。

这是一个可能对您有所帮助的答案:如何将 Android Spinner 创建为弹出窗口?

除了接受的答案中的代码之外,调用setCustomTitle对话框以拥有您自己的样式标题视图或在AlertDialogxml 布局中找到图标视图的 id,如果您只想更改图标,则直接设置图标。

于 2012-10-26T18:29:40.350 回答
0

要更改文本的颜色,您可以尝试使用 HTML

String redText = "This is <font color='red'>Red text</font>.";
mSpinner.setPrompt(Html.fromHtml(redText));

您可以尝试按照本文建议的方式破解标题 如何自定义对话框的标题布局

Window window = mSpinner.getWindow();
window.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_custom_header);

其中 R.layout.my_custom_header 是您定义要在标题中显示的内容的布局。我没有尝试过,只是偏离了我上面引用的帖子中的建议。

但可能你最好的选择是听 Chiru 并制作你自己的自定义视图,让它看起来完全像你想要的那样。

于 2012-10-22T06:41:06.703 回答