|
目录结构
第一步:
/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"
androidrientation="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>
复制代码
效果图:
未展开下拉列表前
展开下拉列表后
选中下拉列表后(选中的是足球)
SpinnerDemo.rar (139.24 KB, 下载次数: 20) |
|