Usage of the following simulated drop-down list
Layout File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="@string/ys"
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="28dip" />
<Spinner android:id="@+id/Spinner01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
SpinnerActivity class
Package com. ljq. sp;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
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 {
Private Spinner sp = null; // drop-down list
Private TextView TV = null;
// Array of all resource Images
Private int [] drawableIds = {R. drawable. football, R. drawable. basketball, R. drawable. volleyball };
// Array of all strings
Private int [] msgIds = {R. string. zq, R. string. SCSI, R. string. pq };
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TV = (TextView) findViewById (R. id. TextView01 );
Sp = (Spinner) this. findViewById (R. id. Spinner01); // initialize the Spinner
Sp. setAdapter (adapter );
Sp. setOnItemSelectedListener (new OnItemSelectedListener (){
Public void onItemSelected (AdapterView <?> Parent, View view, int positon, long id ){
LinearLayout ll = (LinearLayout) view;
View v = ll. getChildAt (0); // obtain the first control ImageView
Log. I ("ljq", v. getClass (). getName ());
TextView tvn = (TextView) ll. getChildAt (1); // obtain the second control TextView
StringBuilder sb = new StringBuilder ();
Sb. append (getResources (). getText (R. string. ys). append (":"). append (tvn. getText ());
TV. setText (sb. toString ());
}
Public void onNothingSelected (AdapterView <?> Parent ){
}
});
}
Private BaseAdapter adapter = new BaseAdapter (){
Public int getCount (){
Return drawableIds. length;
}
Public Object getItem (int position ){
Return drawableIds [position];
}
Public long getItemId (int position ){
Return position;
}
Public View getView (int position, View convertView, ViewGroup parent ){
LinearLayout ll = new LinearLayout (SpinnerActivity. this );
Ll. setOrientation (LinearLayout. HORIZONTAL );
ImageView iv = new ImageView (SpinnerActivity. this );
Iv. setImageResource (drawableIds [position]);
Ll. addView (iv );
TextView TV = new TextView (SpinnerActivity. this );
TV. setText (msgIds [position]); // sets the content
TV. setTextSize (24 );
TV. setTextColor (R. color. black );
Ll. addView (TV );
Return ll;
}
};
}
Running result