1)布局文件main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MyActivity" >
<TextView
android:text="@string/ys"
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="28sp"
/>
<Spinner
android:id="@+id/Spinner01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
2)MainActivity
package com.howell.sample6_3;
import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class MyActivity extends Activity {
final static int WRAP_CONTENT = -2;
int [] drawableIds = {R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5};
int [] msgIds = {R.string.p1, R.string.p2, R.string.p3, R.string.p4, R.string.p5};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner sp = (Spinner)this.findViewById(R.id.Spinner01);
BaseAdapter ba = new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll = new LinearLayout(MyActivity.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ImageView ii = new ImageView(MyActivity.this);
ii.setImageDrawable(getResources().getDrawable(drawableIds[position]));
ii.setLayoutParams(new Gallery.LayoutParams(60, 60));
ll.addView(ii);
TextView tv = new TextView(MyActivity.this);
tv.setText(" " + getResources().getText(msgIds[position]));
tv.setTextSize(24);
// tv.setTextColor(getResources().getColorStateList(R.color.black));
tv.setTextColor(Color.rgb(0, 0, 0));
ll.addView(tv);
return ll;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return msgIds.length;
}
};
sp.setAdapter(ba);
sp.setOnItemSelectedListener(
new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
TextView tv = (TextView)findViewById(R.id.TextView01);
LinearLayout ll = (LinearLayout)arg1;
TextView tvn = (TextView)ll.getChildAt(1);
StringBuilder sb = new StringBuilder();
sb.append(getResources().getText(R.string.ys));
// sb.append(":");
sb.append(tvn.getText());
tv.setText(sb.toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
);
}
}