标签:
定义出第一个图片的布局和弹出框(一个listView)的布局,,这里就不在多说了~ListView需要自己定义一个MyspinnerAdapter~做好这些准备之后,就是弹出框的实现了~
protected void onCreate(Bundle savedInstanceState) { |
03 |
super .onCreate(savedInstanceState); |
04 |
setContentView(R.layout.activity_main); |
05 |
textView = (TextView) findViewById(R.id.text); |
06 |
imgView = findViewById(R.id.arrowbut); |
07 |
// 实例化一个List,添加数据 |
08 |
list = new ArrayList<String>(); |
09 |
list.add( "第一展厅" ); |
10 |
list.add( "第二展厅" ); |
11 |
list.add( "第三展厅" ); |
12 |
// 实例化一个适配器,list的数据作为Adapter的数据 |
13 |
adapter = new MyspinnerAdapter( this , list); |
14 |
// 默认设置下拉框的标题为数据的第一个 |
15 |
textView.setText((CharSequence) adapter.getItem( 0 )); |
16 |
spinnerlayout = (LinearLayout) findViewById(R.id.spinnerid); |
17 |
// 点击右侧按钮,弹出下拉框 |
18 |
imgView.setOnClickListener( new OnClickListener() { |
19 |
20 |
@Override |
21 |
public void onClick(View v) { |
22 |
23 |
showWindow(v); |
24 |
25 |
} |
26 |
}); |
27 |
} |
28 |
29 |
@SuppressWarnings ( "deprecation" ) |
30 |
public void showWindow(View v) { |
31 |
// 找到布局文件 |
32 |
layout = (LinearLayout) LayoutInflater.from( this ).inflate(R.layout.mypinner_dropdown, null ); |
33 |
// 实例化listView |
34 |
listView = (ListView) layout.findViewById(R.id.listView); |
35 |
// 设置listView的适配器 |
36 |
listView.setAdapter(adapter); |
37 |
// 实例化一个PopuWindow对象 |
38 |
popupWindow = new PopupWindow(v); |
39 |
// 设置弹框的宽度为布局文件的宽 |
40 |
popupWindow.setWidth(spinnerlayout.getWidth()); |
41 |
// 高度随着内容变化 |
42 |
popupWindow.setHeight(LayoutParams.WRAP_CONTENT); |
43 |
// 设置一个透明的背景,不然无法实现点击弹框外,弹框消失 |
44 |
popupWindow.setBackgroundDrawable( new BitmapDrawable()); |
45 |
// 设置点击弹框外部,弹框消失 |
46 |
popupWindow.setOutsideTouchable( true ); |
47 |
// 设置焦点 |
48 |
popupWindow.setFocusable( true ); |
49 |
// 设置所在布局 |
50 |
popupWindow.setContentView(layout); |
51 |
// 设置弹框出现的位置,在v的正下方横轴偏移textview的宽度,为了对齐~纵轴不偏移 |
52 |
popupWindow.showAsDropDown(v, -textView.getWidth(), 0 ); |
53 |
// listView的item点击事件 |
54 |
listView.setOnItemClickListener( new OnItemClickListener() { |
55 |
56 |
@Override |
57 |
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { |
58 |
// TODO Auto-generated method stub |
59 |
textView.setText(list.get(arg2)); // 设置所选的item作为下拉框的标题 |
60 |
// 弹框消失 |
61 |
popupWindow.dismiss(); |
62 |
popupWindow = null ; |
63 |
} |
64 |
}); |
spinner用法:
this.bank_name = (Spinner) super.findViewById(R.id.spinnerCityStatic);
this.bank_name.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
// 下拉框选择事件
private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String city = parent.getItemAtPosition(position).toString();
Toast.makeText(BangBankActivity.this, "选择的城市是:" + city,
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
Android UI自定义Spinner下拉框(用popuwindow实现)-转
标签:
原文地址:http://www.cnblogs.com/manmanlu/p/4355705.html