自定义Android的Spinner
来源:互联网 发布:非球面加工软件 编辑:程序博客网 时间:2024/05/16 02:09
刚接触Android平台编程不久,被烂教材整得头晕。
有一个关于Spinner扩展的问题,问题是,怎么把弹出来的菜单自定义。当然,书上是没有告诉你的,它们压根就不知道那个和ArrayAdapter相关的两个layout关联到底是什么回事说清楚,就这么列了个代码就算了。我很头晕,于是,写代码,验证自己的推测,搞了好久才“哦”一声解决了自己的疑问。
而自定义Spinner则是和这两个layout有关,ArrayAdapter中初始化的那个layout,是用来渲染spinner被选择的item的样式,而setDropDownResource这个layout,则是弹出框时用的item样式。也就是说,这个spinner使用了两个列表,只不过第一个列表只是显示了一行而已。
下面列个代码,大家仔细看就明白了(因为中午没睡,现在太困了,没精力写为什么了,估计大家能看得懂)。
01
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02
03
<
LinearLayout
xmlns: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
<
ImageView
android: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
<
TextView
android: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
>
01
public
class
CustomSpinnerActivity
extends
Activity {
02
public
Spinner spinner;
03
04
public
void
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
public
View 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
return
view;
28
}
29
};
30
spinner.setAdapter(adapter);
31
}
32
33
}
- 自定义Android的Spinner
- 自定义Android的Spinner
- 自定义Android的Spinner
- 自定义Android的Spinner
- 自定义Android的Spinner
- 自定义Android的Spinner
- 自定义Android的Spinner
- android实践项目七自定义的Spinner
- Android 实现自定义Spinner
- android自定义Spinner
- Android 实现自定义Spinner
- Android自定义Spinner
- android之自定义Spinner
- Android 实现自定义Spinner
- Android 实现自定义Spinner
- Android 自定义Spinner
- Android 自定义 spinner样式
- Android自定义Spinner
- list按某定值取数据
- 我们工作到底为了什么(这篇文章很重要)----强烈推荐
- Windows 8应用程序,系统管理模块图片展示
- iPhone开发:给UIView设置边框和阴影
- 安装lighttpd lighttpd随linux启动
- 自定义Android的Spinner
- PL/SQL 9.0.6的激活码
- 在cocos2d里面如何拖拽精灵
- 午睡过后没了感觉
- 新标签接的设置说明(适合3120换3150的客户)
- 红外发射与接收原理
- 设计的软件测试用例是否越详细越好?
- poj 1152
- 数据库连接方式