Custom Android Spinner

Source: Internet
Author: User
Tags getcolor

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 <?xmlversion="1.0"encoding="utf-8"?>
02  
03 <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
04               android:layout_width="fill_parent"
05               android:layout_height="fill_parent"
06               android:padding="10dip">
07  
08     <ImageViewandroid: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"/>
14  
15     <TextViewandroid: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"/>
21 </LinearLayout>
01 public classCustomSpinnerActivity
extendsActivity {
02     publicSpinner spinner;
03  
04     publicvoid
onCreate(Bundle savedInstanceState) {
05         super.onCreate(savedInstanceState);
06         setContentView(R.layout.ch07_custom_spinner);
07  
08         Resources res = getResources();
09         CharSequence[] platforms = res.getTextArray(R.array.platforms);
10  
11         spinner = (Spinner) findViewById(R.id.target);
12  
13         ArrayAdapter<CharSequence> adapter =new
ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, platforms) {
14             @Override
15             publicView getDropDownView(int
position, View convertView, ViewGroup parent) {
16                 View view = getLayoutInflater().inflate(R.layout.ch07_custom_spinner_item, parent,false);
17  
18                 TextView label = (TextView) view.findViewById(R.id.label);
19                 label.setText(getItem(position));
20  
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);
25                 }
26  
27                 returnview;
28             }
29         };
30         spinner.setAdapter(adapter);
31     }
32  
33 }

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.