Android custom spinner text color and display style, androidspinner
On the project page, a few values are not allowed for user input. You can only select a project from the drop-down list. At first you thought about custom dialog or popupwindow, but a lot of code is added, considering that there are already a lot of engineering code, I thought of using the component spinner that has been developed by google. This is a very useful system drop-down option component. I will not talk about the specific usage, many of them have already been summarized. The general process is to set the spinner control first, as shown below:
Spinner <android: id = "@ + id/touchprice" android: layout_width = "190dp" android: layout_height = "35dp" android: layout_marginLeft = "10dp" android: dropDownWidth = "20dp" android: prompt = "@ string/touch"/> <! -- Set the value that is displayed first -->
Then, you can set the default drop-down style and pop-up mode in the code to listen to the selected options. There are many examples for reference. My example is as follows:
// 2nd drop-down lists, with the effective date (spinner) view. findViewById (R. id. required tivedate); // connect the optional content to the ArrayAdapter adapter = new ArrayAdapter <String> (getActivity (), android. r. layout. simple_spinner_item, m); </span> </strong> // you can specify the style adapter in the drop-down list. setDropDownViewResource (android. r. layout. simple_spinner_dropdown_item); // Add the adapter to the spinner. setAdapter (adapter); // Add Event Events to listen to the Spinner event. setOnItemSelectedListener (n Ew SpinnerSelectedListener (); // sets the default value of spinner. setVisibility (View. VISIBLE);} // operates class SpinnerSelectedListener implements using arrays {@ Overridepublic void onItemSelected (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {Toast tst = Toast. makeText (getView (). getContext (), "you clicked" + m [arg2], Toast. LENGTH_SHORT); tst. show () ;}@ Overridepublic void onNothingSelected (AdapterView <?> Arg0 ){}}
In this way, the most basic system drop-down list components are implemented as follows:
Is it really ugly? In particular, if my edittext below is to be displayed with rounded corners, it seems too big to be compared, so it is a key step, do not use the built-in list item layout file, that is, the above Code:
// Connect the optional content with ArrayAdapter adapter = new ArrayAdapter <String> (getActivity (), android. r. layout. simple_spinner_item, m); // you can specify the style of the drop-down list.
We can replace it with the layout format we want to display. below is what I want to display, which is the rounded corner. Then we can change the color of the default font to white and center the display:
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 to set the drop-down text style, If You Want To rounded corner, you must set android: background in the spinner component to set the rounded corner style, as shown below:
<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"/>
Code of the file with rounded corners pasted. tipspriceshape. xml is 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 an arc --> <! -- Android: radius arc radius --> <corners android: radius = "@ dimen/RoundedAmplitude"/> <gradient android: angle = "270" android: centerColor = "# 2B2B2B" android: endColor = "# 2B2B2B" android: startColor = "# 2B2B2B"/> <stroke android: width = "0.01dp" android: color = "# FFFFFF"> </stroke> </shape>
In this way, the demand is successfully completed, the default style is changed to the rounded corner, and the text is centered and the color is white. As follows:
(Red is required for confidentiality. You are welcome to point out the shortcomings or better solutions ).
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.