自定义Spinner样式
通常情况下spinner都采用系统默认的android.R.layout.simple_spinner_item和android.R.layout.simple_spinner_dropdown_item
有时候也需要自定义这两个文件
比如我这里是spinner_item.xml,这个是下拉列表的item样式
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dip" android:gravity="center_vertical" > <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ico_hook" android:layout_marginLeft="20dp" android:visibility="invisible" /> <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/gray3" android:textSize="@dimen/font_big" android:layout_marginLeft="20dp" /> </LinearLayout>
另一个是:spinner.xml,注意这里的CheckedTextViewid必须是@android:id/text1
<?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/black" android:textSize="@dimen/font_xbig" android:singleLine="true" />
这个是控制spinner样式的。
至于spinner的背景可以这样控制:spinner_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@drawable/spinner_pressed" /><!--按下时效果--> <item android:state_pressed="false" android:drawable="@drawable/spinner_normal" /><!--默认效果--> </selector>
引用背景:
<Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/spinner_selector" />
至于怎么使用spinner_item.xml和spinner.xml两个文件看下面的代码:
需要重写ArrayAdapter的getDropDownView(intposition,ViewconvertView,ViewGroupparent)方法。
ArrayList<String> list = new ArrayList<String>(); list.add("1人"); list.add("2人"); list.add("3人"); list.add("4人"); list.add("5人"); list.add("6人"); list.add("7人"); list.add("8人"); list.add("9人"); list.add("10人"); spinner=(Spinner)findViewById(R.id.spinner); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.spinner, list){ @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { if(convertView==null){ convertView = getLayoutInflater().inflate(R.layout.spinner_item, parent, false); } TextView label = (TextView) convertView.findViewById(R.id.label); label.setText(getItem(position)); ImageView icon = (ImageView) convertView.findViewById(R.id.icon); icon.setVisibility(spinner.getSelectedItemPosition() == position?View.VISIBLE:View.INVISIBLE); return convertView; } }; spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){ public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub } public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } });
完成之后,类似下面的样子(效果图是我网上找的,我懒得截图了):
点击时:
下拉列表类似:
相关推荐
Mexican 2011-01-20
满城风絮 2011-09-06
ruanjianxiong 2016-09-20
toperfect 2013-03-19
绿豆饼 2020-04-10
狼窝 2020-03-20
yxx0 2015-04-15
fanleiym 2017-02-09
learningITwell 2016-08-18
virusplayer 2015-04-15
Ifree团队 2013-07-08
emptoney 2013-03-19
tufeizhang 2012-11-30
maplele 2012-07-27
najiutan 2012-02-26
Mexican 2012-02-15