如何更改Searchable Spinner在android中列出背景颜色?

时间:2017-04-11 07:20:51

标签: android search automation android-spinner

我想在Android中使用Searchable Spinner。我是通过这个activity_main.xml代码

完成的
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
    android:id="@+id/search_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"/>

MainActivity.java代码是

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.toptoche.searchablespinnerlibrary.SearchableSpinner;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SearchableSpinner searchableSpinner = (SearchableSpinner) findViewById(R.id.search_spinner);
        String[] items = new String[] { "A Tea", "B Tea", "C Tea", "D Tea", "E Tea"};
        ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,R.layout.spinnerlist,R.id.item ,items);
        searchableSpinner.setTitle("Select Item");
        searchableSpinner.setPositiveButton("Cancel");
        searchableSpinner.setAdapter(adapter);
        searchableSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
}

我的spinnerlis.xml代码是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="#C79329"
    android:layout_height="match_parent">
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/item"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:textSize="16dp"
        android:gravity="left"
        android:text="name"
        android:background="#C79329"
        android:textColor="#FFF"
        android:padding="8dp"/>
</LinearLayout>

但我的微调器列表现在是白色的。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

转到您的styles.xml并在主题中添加以下项目行。

Toggle()

您的样式主题将如下所示

<item name="android:alertDialogTheme">@style/AppTheme.SpinnerDialog</item>

之后,创建此样式

<style name="AppTheme" parent="Theme.AppCompat">
    ...
    <item name="android:alertDialogTheme">@style/AppTheme.SpinnerDialog</item>
</style>

这对我有用