Align spinner text to center

2019-05-30 23:26发布

I have a spinner(dropdown)enter image description here

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);

3条回答
甜甜的少女心
2楼-- · 2019-05-31 00:09
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"
    />
查看更多
来,给爷笑一个
3楼-- · 2019-05-31 00:11

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);
查看更多
Emotional °昔
4楼-- · 2019-05-31 00:25

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" />
查看更多
登录 后发表回答