xamarin.androidspinnergravity

center the text in position 0, code problem xamarin


I looked everywhere, but for the moment works.

here is my code:

     <Spinner
                    android:id="@+id/departement"
                    android:layout_width="match_parent"
                    android:layout_height="25dp"
                    android:background="@color/colorAccent"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:gravity="center_vertical|center_horizontal"
                     />


      Spinner departement = (Spinner)sender;
      ((TextView)departement.GetChildAt(0)).SetForegroundGravity(GravityFlags.CenterHorizontal);
      ((TextView)departement.GetChildAt(0)).SetTextColor(Color.White);

the color works, but the center position does not. do you have an idea ?


Solution

  • You can use a new xml to define the content of that item in Spinner.

    And cusotmize the style in that xml .

    Item.xml

    <?xml version="1.0" encoding="utf-8" ?> 
        <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"    //center
        android:textColor="@android:color/holo_red_dark"/>   //Textcolor
    

    Set in Adapter

    ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,Resource.Layout.Item,items);
    departement.Adapter = adapter;