Android Spinner背景颜色和三角形颜色

时间:2016-10-17 14:41:51

标签: java android android-layout

我的应用程序中有一个微调器,我希望自定义背景颜色和自定义三角形颜色。

设置每个不是问题,但组合不起作用。这是我目前的代码

布局:

 <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/languageSpinner"
        android:textAlignment="center"
        />

的活动:

Spinner languageSpinner = (Spinner) findViewById(R.id.languageSpinner);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(this, 
                                              R.layout.item_spinner_lang,
                                              new String[] { "DE", "EN", "SP" });
spinnerArrayAdapter.setDropDownViewResource(R.layout.item_spinner_dropdown_lang);

languageSpinner.setAdapter(spinnerArrayAdapter);
languageSpinner.setSelection(spinnerArrayAdapter.getPosition(prefLang), false);
//languageSpinner.setBackgroundColor(someColor); // if I use this, even the triangle has that color and vanishes
languageSpinner.getBackground().setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);
languageSpinner.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

item_spinner_lang.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:textSize="20sp"
    android:gravity="center"
    android:textColor="#000000"
    android:padding="5dip"
    />

旋转器目前是白色的。但我希望它是灰色的,三角形白色,文字也是白色。怎么做?

1 个答案:

答案 0 :(得分:0)

尝试为微调器使用自定义背景和样式:

adapter.setDropDownViewResource(R.layout.spinner_item_drop_down);

spinner_item_drop_down.xml

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/SpinnerItemStyle"/>

SpinnerItemStyle

<style name="SpinnerItemStyle">
    <item name="android:background">@drawable/spinner_bgd</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:gravity">center_vertical|start</item>
</style>

spinner_bgd.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:width="10dp"
        android:height="10dp">

        <bitmap
            android:gravity="center_vertical|right"
            android:src="@drawable/ic_custom_arrow"
            android:tint="@color/gray" />

    </item>

</selector>