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