自定义Android的Spinner


来自:http://my.oschina.net/kut/blog/30564

刚接触Android平台编程不久,被烂教材整得头晕。

有一个关于Spinner扩展的问题,问题是,怎么把弹出来的菜单自定义。当然,书上是没有告诉你的,它们压根就不知道那个和ArrayAdapter相关的两个layout关联到底是什么回事说清楚,就这么列了个代码就算了。我很头晕,于是,写代码,验证自己的推测,搞了好久才“哦”一声解决了自己的疑问。

而自定义Spinner则是和这两个layout有关,ArrayAdapter中初始化的那个layout,是用来渲染spinner被选择的item的样式,而setDropDownResource这个layout,则是弹出框时用的item样式。也就是说,这个spinner使用了两个列表,只不过第一个列表只是显示了一行而已。

下面列个代码,大家仔细看就明白了(因为中午没睡,现在太困了,没精力写为什么了,估计大家能看得懂)。

01<?xmlversion="1.0"encoding="utf-8"?>
02 
03<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
04              android:layout_width="fill_parent"
05              android:layout_height="fill_parent"
06              android:padding="10dip">
07 
08    <ImageViewandroid:id="@+id/icon"
09               android:src="@drawable/tick"
10               android:layout_marginRight="10dip"
11               android:layout_width="wrap_content"
12               android:layout_height="wrap_content"
13               android:visibility="invisible"/>
14 
15    <TextViewandroid:id="@+id/label"
16              android:layout_width="wrap_content"
17              android:layout_height="wrap_content"
18              android:layout_toRightOf="@id/icon"
19              android:textColor="#000000"
20              android:textSize="22dip"/>
21</LinearLayout>
01public classCustomSpinnerActivity extendsActivity {
02    publicSpinner spinner;
03 
04    publicvoid onCreate(Bundle savedInstanceState) {
05        super.onCreate(savedInstanceState);
06        setContentView(R.layout.ch07_custom_spinner);
07 
08        Resources res = getResources();
09        CharSequence[] platforms = res.getTextArray(R.array.platforms);
10 
11        spinner = (Spinner) findViewById(R.id.target);
12 
13        ArrayAdapter<CharSequence> adapter =new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, platforms) {
14            @Override
15            publicView getDropDownView(int position, View convertView, ViewGroup parent) {
16                View view = getLayoutInflater().inflate(R.layout.ch07_custom_spinner_item, parent,false);
17 
18                TextView label = (TextView) view.findViewById(R.id.label);
19                label.setText(getItem(position));
20 
21                if(spinner.getSelectedItemPosition() == position) {
22                    label.setTextColor(getResources().getColor(R.color.selected_fg));
23                    view.setBackgroundColor(getResources().getColor(R.color.selected_bg));
24                    view.findViewById(R.id.icon).setVisibility(View.VISIBLE);
25                }
26 
27                returnview;
28            }
29        };
30        spinner.setAdapter(adapter);
31    }
32 
33}

效果图

btw:我也来接触android了?没办法,为了应付GCD,曲线救国,希望能赶得上,还有整整四个月时间让我去深入这一领域。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值