这个需求其实还是有的,有时候我们想在Spinner中制作出一个类似于TextView的hint那样的效果。
如下:
当你第一次看到这个Spinner的时候,其中显示“点击选择”,而你点了一下,就会出现选项,一旦点选了某个选项,“点击选择”就会从此消失,也就是说,那只是提示,不参与逻辑判断。
那么如何做到这种效果呢?我去stackoverflow上查了一下,发现http://stackoverflow.com/questions/6602339/android-spinner-hint#,这个问题下的回答比较好——
也就是boni2k提供的solution,于是我复制过来做了一些自己的测试和理解如下:
代码部分:
// 布局文件 R.layout.spinner_showed_item 决定了spinner中被显示出的那条的样式
// 这个布局文件有一个默认安卓布局,名字叫做,自己写布局的时候不妨前去参考其中的各种要求
// 例如必须是一个CheckedTextView 等等,事实上,顶多也就自己改一个背景图片之类的
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_showed_item) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.w(TAG_CRY, "It is getView ing!");
View v = super.getView(position, convertView, parent);
//下面这句if似乎注释掉也不会产生影响
// if (position == getCount()) {
// Log.w(TAG_CRY, "NOW : position == getCount()");
// ((CheckedTextView)v.findViewById(R.id.spinner_showed_text)).setText("被显示的默认文字");
// ((CheckedTextView)v.findViewById(R.id.spinner_showed_text)).setHint("被显示的默认提示"); //"Hint to be displayed"
// }
return v;
}
@Override
public int getCount() {
Log.w(TAG_CRY, "获得getCount为:" + super.getCount());
return super.getCount()-1; // you don't display last item. It is used as hint.
}
};
adapter.setDropDownViewResource(R.layout.spinner_option_items);
adapter.add(getString(R.string.option_find_subject_math));
adapter.add(getString(R.string.option_find_subject_chinese));
adapter.add(getString(R.string.option_click_to_choose));
Spinner sp = (Spinner) view.findViewById(R.id.sp_Subject);
sp.setAdapter(adapter);
sp.setSelection(adapter.getCount()); //display hint
以上中LOG是我的测试,因为我还并不清楚adapter里的这几个被重写的函数是什么意思,并且暂时不打算去弄明白(虽然很重要)。注意他那句if,我发现去掉也一样正常工作,所以并不是很清楚这句的含义,看懂的朋友请在下面评论交流一下。
getCount()-1会使得Spinner一直认为自己本来就少一条,所以除非你在代码里去setSelection(adapter.getCount()),否则这个Spinner永远不会自动显示出最后那条。我们就是利用这个原理,将最后一条写成我们想设的hint,就达到了目的。
我努力试了一下LOG打印出getview 和 getcount的信息,看他们到底什么时候被调用,不过并没有发现什么规律,文档也看的不是很明白,这个问题放到以后再细究吧。
接下来,我要讲一下怎么设置Spinner的布局文件,设成自己的样式,比如你可能想自己修改背景图片之类的。
上面代码里已经提到了
R.layout.spinner_showed_item
和
R.layout.spinner_option_items
将R.layout.spinner_showed_item背景色设成黄色,xml文件如下:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_showed_text"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:background="#ff0"
android:layout_height="match_parent"
android:ellipsize="marquee"/>
将 R.layout.spinner_option_items背景色设成蓝色,xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_option_text"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:background="#0ff"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ellipsize="marquee"/>
就会达到如下这种效果,一目了然:
我再贴上安卓系统本身自带的Spinner布局文件android.R.layout.simple_spinner_dropdown_item,以供我们自己写的时候参考,注意其中的注释说明,还是挺有用的。
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License")
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"/>