Android Custom Spinner text color and display style

Source: Internet
Author: User

Project interface, there are several values do not allow user input, can only select the project in the drop-down list, the first thought of custom dialog or Popupwindow, but will add a lot of code, considering the current project code is much, So think of the use of Google has developed components spinner components, which is a very useful system drop-down options components, specific usage I will not say, a lot has been summed up, the approximate process is to set up the spinner control, as follows:

spinner<                android:id= "@+id/touchprice"                android:layout_width= "190DP"                android:layout_height= "35DP"                android:layout_marginleft= "10DP"                android:dropdownwidth= "20DP"                android:prompt= "@string/touch"/>   <!--here set which value to display first--

After that you can set the default drop-down style and pop-up mode in the code, listen to the selected options, there are many examples to refer to, mine is this:

2nd drop-down list, valid date Spinner = (Spinner) View.findviewbyid (r.id.effectivedate);//link optional content to arrayadapter adapter = new Arrayadapter<string> (Getactivity (), Android. R.layout.simple_spinner_item, m); </span></strong>////Set the style of the drop-down list Adapter.setdropdownviewresource ( Android. R.layout.simple_spinner_dropdown_item);//Add Adapter to spinner Spinner.setadapter (adapter);// Add Event Spinner Event Listener Spinner.setonitemselectedlistener (New Spinnerselectedlistener ());//Set Default value Spinner.setvisibility ( view.visible);} Use array form to manipulate class Spinnerselectedlistener implements Onitemselectedlistener {@Overridepublic void onitemselected ( Adapterview<?> arg0, View arg1, int arg2,long arg3) {Toast TST = Toast.maketext (GetView (). GetContext (), "you clicked" + M[ar G2], Toast.length_short); Tst.show ();} @Overridepublic void onnothingselected (adapterview<?> arg0) {}}

This enables the most basic system drop-down list components, as follows:



is not feeling particularly ugly, especially the following below my edittext is to use the fillet to display, it appears that the contrast is too big, so it is a key step, do not use the system comes with the list item layout file, which is the above code:

Connect the optional content to the Arrayadapter adapter = new Arrayadapter<string> (Getactivity (), Android. R.layout.simple_spinner_item, m);///Set the style of the drop-down list
We can replace the layout format we want to show, here's what I'm going to look like, the rounded corners, and then change the color of the default font inside, to white and centered:

Activity_tipsprice_spinner.xml

<?xml version= "1.0" encoding= "Utf-8"? ><textview xmlns:android= "http://schemas.android.com/apk/res/ Android "      android:id=" @android: Id/text1 "      android:gravity=" center "      android:paddingtop=" 5DP "    Android:textstyle= "Bold"      android:textcolor= "#FFFFFF"      android:textsize= "14DP"      android:singleline= " True "      android:layout_width=" match_parent "      android:layout_height=" wrap_content "      android:ellipsize=" Marquee "/>  

This is just a drop-down text style, and if you want a fillet, you must set the style of the fillet in the Spinner component settings Android:background, as follows:

<spinner
Android:id= "@+id/touchprice"
Android:layout_width= "190DP"
android:layout_height= "35DP"
android:layout_marginleft= "10DP"
android:background= "@drawable/tipspriceshape"
Android:dropdownwidth= "20DP"
android:prompt= "@string/touch"/>


The file code with rounded corners istipspriceshape.xml as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"    android:shape= "Rectangle" >    <!--fill Color--    <solid android:color= "#2B2B2B"/>    <!- -Set the four corners of the button to arc-    <!--android:radius arc Radius--    <corners android:radius= "@dimen/roundedamplitude "/>    <gradient        android:angle=" android:centercolor= "        #2B2B2B" android:endcolor= "        # 2b2b2b "        android:startcolor=" #2B2B2B "/>    <stroke        android:width=" 0.01DP "        android:color= "#FFFFFF" >         </stroke></shape>
This completes the requirement smoothly, changes the default style to rounded corners, and the text is centered and the color is white. As follows:



(Red for the sake of secrecy, you are welcome to point out the shortcomings, or better way).

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Custom Spinner text color and display style

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.