Spinner位于 android.widget包下,每次只显示用户选中的元素,当用户再次点击时,会弹出选择列表供用户选择,而选择列表中的元素同样来自适配器。Spinner是View类得一个子类。
先来看看 运行效果吧。





字符串资源string.xml
<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, Activity01!</string>    <string name="app_name">xh_spinner_test</string>    <string name="ys">您的爱好</string>    <string name="lq">篮球</string>    <string name="zp">足球</string>    <string name="pq">排球</string></resources>


颜色资源 colors.xml
<?xml version="1.0" encoding="utf-8"?><resources>    <color name="red">#fd8d8d</color><color name="green">#9cfda3</color><color name="blue">#8d9dfd</color><color name="white">#FFFFFF</color><color name="black">#000000</color></resources>


布局文件 main.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="fill_parent"    ><TextView      android:text="@string/ys"    android:id="@+id/TextView01"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:textSize="28dip"    /><Spinnerandroid:id="@+id/Spinner01"android:layout_width="fill_parent"android:layout_height="wrap_content"/></LinearLayout>


Activity01类
package xiaohang.zhimeng;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 Activity01 extends Activity {final static int WRAP_CONETNT = -2;// 表示WRAP_CONTENT的常量// 所有资源的图片(足球、篮球、排球) id的数组int[] drawableIds = { R.drawable.football, R.drawable.basketball,R.drawable.volleyball };// 所有资源字符串 (足球、篮球、排球) id的数组int[] msgIds = { R.string.zp, R.string.lq, R.string.pq };@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Spinner sp = (Spinner) findViewById(R.id.Spinner01);BaseAdapter ba = new BaseAdapter() {public int getCount() {// 一共三个选项return 3;}public Object getItem(int position) {return null;}public long getItemId(int position) {return 0;}public View getView(int position, View convertView, ViewGroup parent) {// 动态生成每个下拉项对应的View,每个下拉项View由LinearLayout// 中包含一个ImageView及一个TextView构成// 初始化LinearLayoutLinearLayout ll = new LinearLayout(Activity01.this);ll.setOrientation(LinearLayout.HORIZONTAL);// 初始化ImageViewImageView ii = new ImageView(Activity01.this);ii.setImageDrawable((getResources().getDrawable(drawableIds[position])));ll.addView(ii);// 初始化TextViewTextView tv = new TextView(Activity01.this);tv.setText(" " + getResources().getText(msgIds[position]));tv.setTextColor(R.color.black);tv.setTextSize(24);ll.addView(tv);return ll;}};// 为Spinner设置内容适配器sp.setAdapter(ba);sp.setOnItemSelectedListener(new OnItemSelectedListener() {public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {// 获取主界面TextViewTextView tv = (TextView) findViewById(R.id.TextView01);// 获取当前选中选项对应的LinearLayoutLinearLayout ll = (LinearLayout) view;// 获取其中的TextViewTextView tvn = (TextView) ll.getChildAt(1);// 用StringBuilder动态生成信息StringBuilder sb = new StringBuilder();sb.append(getResources().getText(R.string.ys));sb.append(":");sb.append(tvn.getText());// 信息设置进住界面tv.setText(sb.toString());}public void onNothingSelected(AdapterView<?> parent) {}});}}


更多相关文章

  1. Android基础(八):文件存储
  2. android 安卓APP获取手机设备信息和手机号码的代码示例
  3. Android中获取TextView一行最多能显示几个字
  4. windows下通过git获取 android 源代码
  5. Android(安卓)的WebView长按保存图片
  6. 【Android(安卓)应用开发】Android中使用ViewPager制作广告栏效
  7. Android打开系统文件管理器
  8. Android开发笔记(一百一十七)app省电方略
  9. Alibaba-AndFix Bug热修复框架原理及源码解析

随机推荐

  1. 如何在Ubuntu12.1下安装Android(安卓)NDK
  2. Android(安卓)API 中文 (53) —— BaseAdap
  3. android 8.0后打开wifi热点以及更改热点
  4. TextView设置中文粗体
  5. Android(安卓)webview 使用自定义字体
  6. Android(安卓)简单2D绘图
  7. android 动态修改菜单menu
  8. Android(安卓)Frame动画
  9. Linux下的Android(安卓)+ Eclipse环境搭
  10. Android(安卓)使用Instrumentation进行界