1

我正在尝试以编程方式更改微调器的状态样式。

但是当我按下一个项目时,它们都被改变了!在按下一切都很好之前,颜色很好:

在此处输入图像描述

按下时,所有项目变为绿色: 在此处输入图像描述

我在我的自定义适配器中做到了这一点:

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {

    //getting the views and all the stuff

    StateListDrawable colorStateList = new StateListDrawable();
    colorStateList.addState(new int[] {android.R.attr.state_focused, android.R.attr.state_pressed}, new ColorDrawable(Color.BLACK));
    colorStateList.addState(new int[] {android.R.attr.state_focused                              }, new ColorDrawable(Color.BLUE));
    colorStateList.addState(new int[] {                              android.R.attr.state_pressed}, new ColorDrawable(Color.GREEN));
    colorStateList.addState(new int[] {                                                          }, new ColorDrawable(Color.YELLOW));

    holder.container.setBackgroundDrawable(colorStateList);

    return convertView;
}

这是我的自定义下拉布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_container"
android:layout_width="@dimen/icon_ligne_size"
android:layout_height="@dimen/icon_ligne_size"
android:layout_gravity="left"
android:gravity="left"
android:orientation="horizontal" >

    <TextView
        android:id="@+id/spinner_dir1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:gravity="center_vertical"
        android:textAppearance="@android:style/TextAppearance.Medium" />

    <ImageView
        android:id="@+id/spinner_arrow"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/spinner_dir2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:gravity="center_vertical"
        android:textAppearance="@android:style/TextAppearance.Medium" />

</LinearLayout>

(我知道这很丑,但这只是为了测试)

4

1 回答 1

1

使用下面的代码

getDropDownView

        mTextViewBackgroundListDrawable = new StateListDrawable();
        mTextViewBackgroundListDrawable.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(Color.BLACK));
        mTextViewBackgroundListDrawable.addState(new int[]{android.R.attr.state_focused}, new ColorDrawable(Color.BLACK));
        mTextViewBackgroundListDrawable.addState(new int[]{android.R.attr.state_enabled}, new ColorDrawable(0));

        textView.setBackgroundDrawable(mTextViewBackgroundListDrawable);

在自定义适配器中全局声明“mTextViewBackgroundListDrawable”变量。

它解决了我的问题。希望这有帮助。

于 2015-04-24T13:56:13.780 回答