Let the text in the spinner center

Source: Internet
Author: User

If you apply Simple_spinner_item or Simple_spinner_dropdown_item, then directly in spinner

android:gravity= "Center"

It is impossible to center the text in the spinner.

This is because the text in the spinner is actually set in the TextView (refer to: HTTP://WWW.DOUBAN.COM/NOTE/483280526/?TYPE=LIKE#SEP).

At first I thought of one way:

Nesting a linearlayout outside the spinner, setting the android:gravity= "center" so that the spinner is naturally centered. The disadvantage is that you can only set spinner to Wrap_content.

In fact, this can be:

Simple_spinner_item.xml and Simple_spinner_dropdown_item.xml are actually in the sdk\platforms\android-18\data\res\layout, Then we can overwrite them.

For example, to overwrite:

Change the TextView in the gravity, and the problem will be solved.

In addition, there are other methods on StackOverflow:

The component that actually creates the items for the the Spinner adapter. So you should customize it (by overriding the getView() method) to return centered TextView widgets.

In your case, replace the new ArrayAdapter<String> ... initialization with this code:

Arrayadapter<String>DataAdapter= New Arrayadapter<String> (This,Android.R.Layout.Simple_spinner_item, Nomprojets){ @Override Public ViewGetView(IntPosition, ViewConvertview, ViewGroupParent) { ReturnSetcentered(Super.GetView(Position,Convertview,Parent)); } @Override Public ViewGetdropdownview(IntPosition, ViewConvertview, ViewGroupParent) { ReturnSetcentered(Super.Getdropdownview(Position,Convertview,Parent)); } Private ViewSetcentered(ViewView) { textview TextView =  ( Span class= "Typ" >textview) view. (android. R. Id. Text1 Textview. (gravity. Center return View; }};            

To center text in spinner

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.