Android custom Spinner font, color, size, and androidspinner

Source: Internet
Author: User

Android custom Spinner font, color, size, and androidspinner

Yesterday, I encountered confusion about the color, Font, and size of the Spinner in my project. So I will summarize this knowledge today. This is actually very simple. To change the font, color, and size, simply do not use the style that comes with the system. Write a layout call with your own exceptions. That's what I do. In this example, you can also use the following two methods to bring up a menu: drop-down box and pop-up box. You can load data sources in two ways. The following describes the code.

This project Source Code address: http://download.csdn.net/detail/qq_16064871/8583085

Reprinted please indicate the source: http://blog.csdn.net/qq_16064871

Package com. example. customspinerdemo; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemSelectedListener; import android. widget. arrayAdapter; import android. widget. spinner; import android. widget. toast; public class MainActivity ex Tends Activity implements OnItemSelectedListener {private Spinner mspinner1; private Spinner mspinner2; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mspinner1 = (Spinner) findViewById (R. id. spinner1); if (mspinner1! = Null) {mspinner1.setOnItemSelectedListener (this);} mspinner2 = (Spinner) findViewById (R. id. spinner2); if (mspinner2! = Null) {mspinner2.setOnItemSelectedListener (this);} // here the two layout self-defined effects do not need to be provided by the system. // manually add ArrayAdapter <String> spinnerAadapter = new ArrayAdapter <String> (this, R. layout. custom_spiner_text_item, getDataSource (); spinnerAadapter. setDropDownViewResource (R. layout. custom_spinner_dropdown_item); mspinner1.setAdapter (spinnerAadapter); // ArrayAdapter spinnerAadapter2 = ArrayAdapter by loading the data source configured in the xml file. createFromR Esource (this, R. array. spiner2_array, R. layout. custom_spiner_text_item); spinnerAadapter2.setDropDownViewResource (R. layout. custom_spinner_dropdown_item); mspinner2.setAdapter (spinnerAadapter2);} public List <String> getDataSource () {List <String> spinnerList = new ArrayList <String> (); spinnerList. add ("Beijing"); spinnerList. add ("Shanghai"); spinnerList. add ("Guangzhou"); spinnerList. add ("Beijing"); spinnerList. add ("Shanghai"); spinnerList. Add ("Guangzhou"); return spinnerList;} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic void onItemSelected (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {if (arg0.getId () = R. id. spinner1) {String itemString = mspinner1.getItemAtPosition (arg2 ). toString (); Toast. makeText (this, "you selected" + itemString, Toast. LENGTH_SHORT ). show (); return;} else if (arg0.getId () = R. id. spinner2) {String itemString2 = mspinner2.getItemAtPosition (arg2 ). toString (); Toast. makeText (this, "you selected" + itemString2, Toast. LENGTH_SHORT ). show (); retu Rn ;}@ Overridepublic void onNothingSelected (AdapterView <?> Arg0 ){}}

Custom_spiner_text_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/textViewCustom"    style="?android:attr/spinnerItemStyle"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:ellipsize="marquee"    android:singleLine="true"    android:textAlignment="inherit"    android:textColor="#222288"    android:textSize="16sp" />

Custom_spinner_dropdown_item.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/checkedTextViewCustom"    style="?android:attr/spinnerDropDownItemStyle"    android:layout_width="match_parent"    android:layout_height="50dp"    android:ellipsize="marquee"    android:singleLine="true"    android:textAlignment="inherit"    android:textColor="#222288"    android:textSize="16sp" />

Activity_main.xml

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "60dp" android: gravity = "center_vertical"> <TextView android: id = "@ + id/textView1" android: layout_width = "116dp" android: layout_height = "wrap_content" android: text = "Custom drop-down box:" android: textSize = "16sp"/> <Spinner android: id = "@ + id/spinner1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1"/> </LinearLayout> <linelayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: gravity = "center_vertical"> <TextView android: id = "@ + id/textView2" android: layout_width = "116dp" android: layout_height = "wrap_content" android: text = "Custom pop-up box:" android: textSize = "16sp"/> <Spinner android: id = "@ + id/spinner2" android: layout_width = "202dp" android: layout_height = "wrap_content" android: spinnerMode = "dialog"/> </LinearLayout>

When you configure a Spinner in xml, it is in the drop-down box by default.

In the displayed dialog box, add this android: spinnerMode = "dialog"

Use other listeners to trigger the spinner statement: mspinner. receivmclick ();

This project Source Code address: http://download.csdn.net/detail/qq_16064871/8583085

Reprinted please indicate the source: http://blog.csdn.net/qq_16064871



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.