10. android Spinner 下拉列表

目录结构

10. android Spinner 下拉列表_第1张图片

 

第一步:

/SpinnerDemo/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">下拉列表</string> <string name="hobby">你的爱好</string> <string name="basketball">篮球</string> <string name="football">足球</string> <string name="volleyball">排球</string> </resources>

 

第二步:

/SpinnerDemo/res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <color name="black">#000000</color> </resources>

 

第三步:

/SpinnerDemo/res/layout/spinner_layout.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="28dip" android:text="@string/hobby"/> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>

 

第四步:

/SpinnerDemo/src/com/myspinner/demo/SpinnerActivity.java

package com.myspinner.demo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TextView; import android.widget.AdapterView.OnItemSelectedListener; public class SpinnerActivity extends Activity { int[] hobbyStrIds = {R.string.basketball, R.string.football, R.string.volleyball}; int[] hobbyImgIds = {R.drawable.basketball, R.drawable.football, R.drawable.volleyball}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner_layout); Spinner spinner = (Spinner) findViewById(R.id.spinner); SpinnerAdapter adapter = new SpinnerAdapter(); spinner.setAdapter(adapter); //选中监听器 OnItemSelectedListener listener = new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { TextView textView = (TextView) findViewById(R.id.textView); LinearLayout linearLayout = (LinearLayout) view; TextView spinnerTextView = (TextView) linearLayout.getChildAt(1); StringBuilder strBuilder = new StringBuilder(); strBuilder.append(getResources().getText(R.string.hobby)); strBuilder.append(" : "); strBuilder.append(spinnerTextView.getText()); textView.setText(strBuilder); } @Override public void onNothingSelected(AdapterView<?> parent) { } }; spinner.setOnItemSelectedListener(listener); } class SpinnerAdapter extends BaseAdapter { @Override public int getCount() { if(hobbyStrIds.length <= hobbyImgIds.length) { return hobbyStrIds.length; } return hobbyImgIds.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout linearLayout = new LinearLayout(SpinnerActivity.this); linearLayout.setOrientation(LinearLayout.HORIZONTAL); linearLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg)); //设置小图标 ImageView imageView = new ImageView(SpinnerActivity.this); imageView.setImageDrawable(getResources() .getDrawable(hobbyImgIds[position])); linearLayout.addView(imageView); //设置内容 TextView textView = new TextView(SpinnerActivity.this); textView.setText(" " + getResources().getText(hobbyStrIds[position])); textView.setTextSize(24); textView.setTextColor(R.color.black); linearLayout.addView(textView); return linearLayout; } } }

 

第五步:

/SpinnerDemo/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myspinner.demo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SpinnerActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="8" /> </manifest>

 

效果图:

10. android Spinner 下拉列表_第2张图片

 

                    未展开下拉列表前

 

10. android Spinner 下拉列表_第3张图片

                    展开下拉列表后

 

10. android Spinner 下拉列表_第4张图片

               选中下拉列表后(选中的是足球)

 

 

源码下载地址: http://download.csdn.net/source/3203345

 

你可能感兴趣的:(android,String,layout,application,Class,encoding)