Android (Java) Learning Note 96: How to change the font and color of the spinner system

Source: Internet
Author: User

1. First we need to know the spinner system comes with font and color essence:

Native spring controls are unable to change fonts and colors ...

As you can see from the code below ... The red callout shows the default layout for Android.

1Spinner S1 =(Spinner) Findviewbyid (r.id.spinner1);2arrayadapter<charsequence> adapter =Arrayadapter.createfromresource (3                  This, R.array.colors, Android. R.layout.simple_spinner_item);4 Adapter.setdropdownviewresource (android. R.layout.simple_spinner_dropdown_item);5 S1.setadapter (adapter);6 S1.setonitemselectedlistener (7                 NewOnitemselectedlistener () {8                      Public voidonitemselected (9Adapterview<?> Parent, view view,intPositionLongID) {TenShowtoast ("spinner1:position=" + position + "id=" +ID); One                     } A  -                      Public voidOnnothingselected (adapterview<?>parent) { -Showtoast ("spinner1:unselected"); the}

In the above:

Android. R.layout.simple_spinner_item is a standard spinner layout provided by Android
Android. R.layout.simple_spinner_dropdown_item// declares appearance when the control is open: Simple_spinner_dropdown_item provided for the system 
It comes with the system.


By finding the source ... See Android. R.layout.simple_spinner_dropdown_item.xml, as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <!--3 / *//device/apps/common/assets/res/any/layout/simple_spinner_item.xml4 **5 * * Copyright, the Android Open Source Project6 **7 * * Licensed under the Apache License, Version 2.0 (the "License");8 * * do not use this file except in compliance with the License.9 * * Obtain a copy of the License atTen ** One * * http://www.apache.org/licenses/LICENSE-2.0 A ** - * * Unless required by applicable law or agreed to writing, software - * * Distributed under the License is distributed on a "as is" BASIS, the * * without warranties or CONDITIONS of any KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * Limitations under the License. - */ +  - - <Checkedtextviewxmlns:android= "Http://schemas.android.com/apk/res/android"  + Android:id= "@android: Id/text1" A style= "? Android:attr/spinnerdropdownitemstyle" at Android:singleline= "true" - Android:layout_width= "Fill_parent" - Android:layout_height= "? Android:attr/listpreferreditemheight" - android:ellipsize= "Marquee" />

Inside is actually a checkedtextview, and Checkedtextview, and inherit from TextView. So we can define an XML file of only TextView.

You can optionally set the properties of the TextView. such as Font ... Color and so on .... Then replace the Adapter.setdropdownviewresource (Android. r.layout.simple_spinner_dropdown_item); XML ... This will change the font and other properties ...

So far... I believe everyone should be able to understand it ...



2. With the above logical thinking, the following things are to be specifically implemented:
(1) We found the Layout folder in the Android project file and created an additional Layout.xml file in this folder as follows:
Here I'm named Custom_spiner_text_item.xml.


here custom_spiner_text_item.xml file contents are as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?>2  <TextViewxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:id= "@+id/textviewcustom"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Wrap_content"6 android:ellipsize= "Marquee"7 Android:singleline= "true"8 android:textalignment= "Inherit"9 Android:textcolor= "#222288"Ten android:textsize= "20SP" />

Here I define the attributes I want, and of course you can define the attributes you want, and sketch out the TextView you want to have.

(2) At this time we have defined the Custom_spiner_text_item.xml, then of course how to use it, as follows:

Believe that using spinner this control friend will not be unfamiliar with the following code statement: Here I am intercepting part of the code to illustrate the use of custom_spiner_text_item.xml.

1List<scanresult> locallist = mainactivity. This. Wifi.getscanresults ();//used to get all the WiFi information scanned by the phone2           if(Locallist.size () < 1)//determines the number of elements in the list. Locallist.size () <1: Indicates that the list has no elements, the number of elements is: Search to the number of wireless networks3          {4Mainactivity. This. connectbtn.setenabled (false);5Toast.maketext (mainactivity. This. Getapplicationcontext (), "No wireless network found", 1). Show ();6           return;7           }8Spinner Localspinner = (Spinner) mainactivity. This. Findviewbyid (r.id.spinner1);9Arraylist<string> localarraylist =NewArraylist<string>();TenIterator<scanresult> Localiterator =Locallist.iterator ();  One         while(true) A          { -           if(!(Localiterator.hasnext ())) -            {     the                //Configure the Spinner control to show styles, spinner just host multiple data, and here's how to present the data -Arrayadapter<string> Localarrayadapter =NewArrayadapter<string> (mainactivity. This. Getapplicationcontext (),

an Droid. R.layout.simple_spinner_item , localarraylist); - Localarrayadapter.setdropdownviewresource (r.layout.custom_spiner_text_item); - + Localspinner.setadapter (localarrayadapter); -Localspinner.setprompt ("Please select Search to network:"); +Mainactivity. This. connectbtn.setenabled (true); A return; at}
}

That's it,R.layout.custom_spiner_text_item means calling the Custom_spiner_text_item.xml resource under the Layout folder

This will reach our needs, hey, is not very simple!

Android (Java) Learning Note 96: How to change the font and color of the spinner system

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.