改变微调器的外观

时间:2016-09-18 05:26:20

标签: android spinner android-spinner

我想将微调器的外观改为这样的 - enter image description here

目前,它看起来像这样 -

enter image description here

我需要做出哪些改变?

这是我的xml代码 -

<Spinner
    android:id="@+id/country_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textview5"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="24dp"
    android:spinnerMode="dialog"
    android:prompt="@string/choose_your_country"
    />

在主要活动中 -

final ArrayList<Country> countries = addAllCountries();
    countrySpinner = (Spinner) findViewById(R.id.country_spinner);
    CountrySpinnerAdapter adapter = new CountrySpinnerAdapter(MainActivity.this, countries);
    countrySpinner.setAdapter(adapter);

1 个答案:

答案 0 :(得分:0)

build.gradle文件

中添加此依赖项
compile 'com.github.rey5137:material:1.2.2'

Xml中编写此代码。

 <com.rey.material.widget.Spinner
                    android:id="@+id/spinner_label"
                    style="@style/LightSpinner"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:minWidth="128dp"
                    android:padding="8dp"
                    app:spn_label="Spinner with arrow" />

在Java类中编写此代码。

Spinner spn_label = (Spinner) findViewById(R.id.spinner_label);
 String[] items = new String[20];
   for (int i = 0; i < items.length; i++) {
          items[i] = "Item " + String.valueOf(i + 1);
       }
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.row_span, items);
 spn_label.setAdapter(adapter);

在布局文件夹中创建 row_span.xml

<?xml version="1.0" encoding="utf-8"?>
<com.rey.material.widget.TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/row_spn_tv"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textDirection="locale"
    style="@style/LightSpinner"/>

输出

enter image description here

有关详细信息,请访问我的答案:Android Spinner showing transparent screen with distortion of data,but items are selectable

并且对于github库,请访问:https://github.com/rey5137/material#donation