From: http://my.oschina.net/kut/blog/30564
Just a short time before getting started with programming on the Android platform, the rotten teaching materials were dizzy.
There is a question about how to customize the menu popped out. Of course, I didn't tell you in the book. They didn't know exactly what the two layout associations related to arrayadapter are. Just put a code in the column. I was dizzy, so I wrote code and verified my speculation. After a long time, I "oh" and solved my doubts.
The custom spinner is related to the two layout. The layout initialized in arrayadapter is used to render the style of the selected item of the spinner, and the layout setdropdownresource is used, it is the item style used in the pop-up box. That is to say, this spinner uses two lists, except that the first list only displays one row.
The following code will show you how to understand it. (because I didn't go to bed at noon, I am so sleepy now. I guess you can understand it if you don't have the energy to write it ).
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
03 |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
04 |
android:layout_width = "fill_parent" |
05 |
android:layout_height = "fill_parent" |
06 |
android:padding = "10dip" > |
08 |
< ImageView android:id = "@+id/icon" |
09 |
android:src = "@drawable/tick" |
10 |
android:layout_marginRight = "10dip" |
11 |
android:layout_width = "wrap_content" |
12 |
android:layout_height = "wrap_content" |
13 |
android:visibility = "invisible" /> |
15 |
< TextView android:id = "@+id/label" |
16 |
android:layout_width = "wrap_content" |
17 |
android:layout_height = "wrap_content" |
18 |
android:layout_toRightOf = "@id/icon" |
19 |
android:textColor = "#000000" |
20 |
android:textSize = "22dip" /> |
01 |
public class CustomSpinnerActivity
extends Activity { |
02 |
public Spinner spinner; |
04 |
public void
onCreate(Bundle savedInstanceState) { |
05 |
super .onCreate(savedInstanceState); |
06 |
setContentView(R.layout.ch07_custom_spinner); |
08 |
Resources res = getResources(); |
09 |
CharSequence[] platforms = res.getTextArray(R.array.platforms); |
11 |
spinner = (Spinner) findViewById(R.id.target); |
13 |
ArrayAdapter<CharSequence> adapter = new
ArrayAdapter<CharSequence>( this , android.R.layout.simple_spinner_item, platforms) { |
15 |
public View getDropDownView( int
position, View convertView, ViewGroup parent) { |
16 |
View view = getLayoutInflater().inflate(R.layout.ch07_custom_spinner_item, parent, false ); |
18 |
TextView label = (TextView) view.findViewById(R.id.label); |
19 |
label.setText(getItem(position)); |
21 |
if (spinner.getSelectedItemPosition() == position) { |
22 |
label.setTextColor(getResources().getColor(R.color.selected_fg)); |
23 |
view.setBackgroundColor(getResources().getColor(R.color.selected_bg)); |
24 |
view.findViewById(R.id.icon).setVisibility(View.VISIBLE); |
30 |
spinner.setAdapter(adapter); |
BTW: I am also in touch with Android? There is no way. In order to cope with GCD and save the country by the curve, I hope I can catch up with it. I still have four months to go deep into this field.