自定义下拉框实现(android)
说道android下拉框spineer,框架中虽有现成的控件,但实际效果可能并不是我们所需要的那种,如下图:
其实我们更需要的是像WEB那种风格,如图所示:
其实实现也很简单,就是自定义个popwindow就可以了
下面贴上代码片段:
public class SpinerWindowDemoActivity extends Activity implements OnClickListener, AbstractSpinerAdapter.IOnItemSelectListener{ /** Called when the activity is first created. */private View mRootView;private TextView mTView;private ImageButton mBtnDropDown;private List<String> nameList = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } private void setupViews(){ mRootView = findViewById(R.id.rootView); mTView = (TextView) findViewById(R.id.tv_value);mBtnDropDown = (ImageButton) findViewById(R.id.bt_dropdown);mBtnDropDown.setOnClickListener(this);String[] names = getResources().getStringArray(R.array.hero_name);for(int i = 0; i < names.length; i++){nameList.add(names[i]);}mSpinerPopWindow = new SpinerPopWindow(this);mSpinerPopWindow.refreshData(nameList, 0);mSpinerPopWindow.setItemListener(this); }@Overridepublic void onClick(View view) {switch(view.getId()){case R.id.bt_dropdown:showSpinWindow();break;}}private void setHero(int pos){if (pos >= 0 && pos <= nameList.size()){String value = nameList.get(pos);mTView.setText(value);}}private SpinerPopWindow mSpinerPopWindow;private void showSpinWindow(){Log.e("", "showSpinWindow");mSpinerPopWindow.setWidth(mTView.getWidth());mSpinerPopWindow.showAsDropDown(mTView);}@Overridepublic void onItemClick(int pos) {setHero(pos);}}
下面附上工程链接:
http://download.csdn.net/detail/geniuseoe2012/5184664
more brilliant,Please pay attention to my CSDN blog -->http://blog.csdn.net/geniuseoe2012