Align spinner text to center

2019-05-30 23:50发布

问题:

I have a spinner(dropdown)

I want to align the text in the spinner to the center and also want to change its colour to white. How can i achieve it???

Spinner.Xml

<Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/sp_age"
                    android:layout_weight="1"
                    style="@style/spinner"
                    android:layout_margin="2dp"
                    android:spinnerMode="dropdown"></Spinner>

Style.Xml

<style name="spinner">

        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:popupBackground">#df382e1c</item>
        <item name="android:layout_marginTop">-12dp</item>
        <item name="android:background">@drawable/dropdown</item>
        <item name="android:textColor">#fff</item>
        <item name="android:textSize">50sp</item>

    </style>

Spinner.Java

gender_array=getResources ().getStringArray (R.array.Gender);
        gender_str= new ArrayAdapter<String> (c,android.R.layout.simple_spinner_dropdown_item,gender_array);
        gender_str.setDropDownViewResource (android.R.layout.simple_dropdown_item_1line);
        gender.setAdapter (gender_str);

回答1:

You have to pass layout on adapter.

Use below code:-

gender_array=getResources ().getStringArray (R.array.Gender);
gender_str= new ArrayAdapter<String> (c,R.layout.spinner_layout,gender_array);
gender_str.setDropDownViewResource (android.R.layout.simple_dropdown_item_1line);
gender.setAdapter (gender_str);

spinner_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinnerTarget"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:paddingLeft="5dp"
    android:maxLines="1"
    android:gravity="center"
    android:singleLine="true"
    android:textColor="@color/white"
    android:textSize="12sp" />


回答2:

Create a separate layout like

<?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:paddingBottom="10dp"
  android:paddingLeft="10dp"
  android:paddingTop="10dp"
  android:minHeight="1dp"
  android:gravity="center"
  android:textColor="@android:color/white" />

and change the two lines

gender_str= new ArrayAdapter<String> (c,android.R.layout.simple_spinner_dropdown_item,gender_array);
gender_str.setDropDownViewResource (android.R.layout.simple_dropdown_item_1line);

to

gender_str= new ArrayAdapter<String> (c,R.layout.your_spinner_layout,gender_array);
gender_str.setDropDownViewResource (R.layout.your_spinner_layout);


回答3:

gender_str= new ArrayAdapter<String>(c,R.layout.check_textview,gender_array);
gender_str.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
gender.setAdapter(gender_str);

check_textview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/txt_spnr"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#ffffff"
    android:paddingLeft="5dp"
    android:maxLines="1"
    android:gravity="center"
    android:singleLine="true"
    android:textColor="@color/black"
    />