Android instance explained spinner style modification

Source: Internet
Author: User

For Android project development, spinner is often used to implement the drop-down box effect. For spinner, there are several ways to load adapters:

1. Direct loading of Android Arrayadapter,simpleadapter;

2. Customize the adapter that inherits the Baseadapter.

For an adapter to load a custom XML layout file, modifying the spinner style is a simple way to modify the displayed style in the defined XML layout file. However, for loading an XML layout file that comes with Android, sometimes the effects that are not required by the project appear. The main problems are the drop-down several:

1.Spinner background display style;

2.Spinner Chinese text box display style;

3.Spinner drop-down menu box display style;


Here are some examples to solve the above-mentioned style problems:

<span style= "FONT-SIZE:18PX;" >package Com.example.spinnerdemo;import Android.app.activity;import Android.os.bundle;import Android.widget.arrayadapter;import Android.widget.spinner;public class Mainactivity extends Activity {private Spinner Spinner;private spinner Spinnertwo;private string[] datas; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); spinner = (spinner) This.findviewbyid ( R.id.spinner) Spinnertwo = (spinner) This.findviewbyid (r.id.spinnertwo);d atas = new string[] {"Zhang San", "John Doe", "Harry", "Zhao Liu"};// The original eco-style to Android. R.layout.simple_spinner_dropdown_item For example, other modifications like arrayadapter<string> adapter = new Arrayadapter<string> ( This,android. R.layout.simple_spinner_dropdown_item, datas); Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item); Spinner.setadapter (adapter);//Custom style changed according to the original ecological style// Spinner Chinese box display style arrayadapter<string> adaptertwo = new Arrayadapter<string> (This,r.layout.my_simple_spinner_self_item, Datas)//spinner drop-down menu display style Adaptertwo.setdropdownviewresource ( R.layout.my_simple_spinner_dropdown_item); Spinnertwo.setadapter (Adaptertwo);}} </span>

By Mainactivity.java in the case of Android.r.layout.simple_spinner_dropdown_item, where Android. The XML layout file for the R.layout.simple_spinner_dropdown_item system itself is as follows:

<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"?><!--/*//device/apps/common/assets/res/any/layout/simple_ spinner_item.xml**** Copyright, the Android Open Source project**** Licensed under the Apache License, Version 2.0 (t He "License"); * * Do not use this file except in compliance with the License. * * Obtain a copy of the License at * * * * * * * * * * unless required by app Licable law or agreed to in writing, software * * Distributed under the License is distributed on a "as is" BASIS, * * with Out warranties or CONDITIONS of any KIND, either express or implied. * * See the License for the specific language governing permissions and * * Limitations under the License.*/--><checke Dtextview xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@android: Id/text1" style= "? Android:attr/spinnerdropdownitemstyle "android:singleline=" true "android:layout_width=" match_parent"android:layout_height="? Android:attr/dropdownlistpreferreditemheight "android:ellipsize=" marquee "Android:textA Lignment= "Inherit"/></span>
and we need to implement the above implementation of the style, just on its basis to modify it.

1. Modify the background color of the spinner itself

A. Setting the background color selector spinner_selector.xml

<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">     <item android:state_pressed=" true "android:drawable=" @color/blue "/>     <item android: State_focused= "true" android:drawable= "@color/blue"/>     <item android:drawable= "@color/white"/> </ Selector> </span>

B. Color settings Color.xml

<span style= "FONT-SIZE:18PX;" ><resources><color name= "White" > #FFFFFF </color><color name= "Blue" > #0000FF </color ></resources></span>
c. Background color setting Activity_main.xml

<span style= "FONT-SIZE:18PX;" ><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:orien tation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=" com.example.spinnerdemo.mainactivity$placeholderfragment "> <Spinner an Droid:id= "@+id/spinner" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android    oid:gravity= "Center_vertical"/> <!--spinner own background color needs to be set: android:background= "@drawable/spinner_selector"- <spinner android:id= "@+id/spinnertwo" android:layout_width= "Match_parent" android:layout_height= "W Rap_content "Android:layout_margintop= "50DP" android:gravity= "center_vertical"/></linearlayout></span> 
2.Spinner Chinese text display style

<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"?><!--/*//device/apps/common/assets/res/any/layout/simple_ spinner_item.xml**** Copyright, the Android Open Source project**** Licensed under the Apache License, Version 2.0 (t He "License"); * * Do not use this file except in compliance with the License. * * Obtain a copy of the License at * * * * * * * * * * unless required by app Licable law or agreed to in writing, software * * Distributed under the License is distributed on a "as is" BASIS, * * with Out warranties or CONDITIONS of any KIND, either express or implied. * * See the License for the specific language governing permissions and * * Limitations under the License.*/--><checke Dtextview xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@android: Id/text1" style= "? Android:attr/spinnerdropdownitemstyle "android:textcolor=" #0000FF "android:gravity=" Center "AndroidOid:singleline= "true" android:layout_width= "Match_parent" android:layout_height= "40DP" android:ellipsize= "marquee "Android:textalignment=" Inherit "android:background=" #FFFFFF "/></span>

3.Spinner drop-down box display style
<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"?><!--/*//device/apps/common/assets/res/any/layout/simple_ spinner_item.xml**** Copyright, the Android Open Source project**** Licensed under the Apache License, Version 2.0 (t He "License"); * * Do not use this file except in compliance with the License. * * Obtain a copy of the License at * * * * * * * * * * unless required by app Licable law or agreed to in writing, software * * Distributed under the License is distributed on a "as is" BASIS, * * with Out warranties or CONDITIONS of any KIND, either express or implied. * * See the License for the specific language governing permissions and * * Limitations under the License.*/--><checke Dtextview xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@android: Id/text1" style= "? Android:attr/spinnerdropdownitemstyle "android:layout_width=" Match_parent "android:layout_height=" 40DP "android:background=" #00FF00 "android:ellipsize=" marquee "android:gravity=" center "android:singleline=" true "Android:textalignment=" Inherit "android:textcolor=" #FF0000 "android:textsize=" 24SP "/></span>

Above is all the contents of the spinner style settings, you can try it.
Source Address: http://download.csdn.net/detail/a123demi/7931263

Android instance explained spinner style modification

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.