将微调文本对齐到中心

时间:2014-05-21 07:05:09

标签: android spinner

我有一个微调器(下拉列表)enter image description here

我想将微调器中的文本与中心对齐,并且还希望将其颜色更改为白色。 我怎样才能实现呢???

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 个答案:

答案 0 :(得分:2)

您必须在适配器上传递布局。

使用以下代码: -

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" />

答案 1 :(得分:0)

创建一个单独的布局,如

<?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" />

并更改两行

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_str= new ArrayAdapter<String> (c,R.layout.your_spinner_layout,gender_array);
gender_str.setDropDownViewResource (R.layout.your_spinner_layout);

答案 2 :(得分:0)

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"
    />