单击Spinner项后SeekBar更改主题

时间:2018-01-09 13:56:10

标签: android xml seekbar

我正在使用自己的Spinner和自定义SeekBar。在API 23或更高版本上,一切工作都很完美,但在Android低于22的情况下,总计不起作用。 点击EditText Spinner更改值的值并像魅力一样工作,更改Spinner选择的值会更改SeekBar主题。 Spinner没有任何动作。

<SeekBar
    android:id="@+id/seekBarVolt"
    android:layout_width="0dp"
    android:layout_height="24dp"
    android:layout_weight="1"
    android:enabled="false"
    android:progressDrawable="@drawable/custom_seekbar"
    android:thumb="@drawable/custom_seekbar_thumb" />

点击Spinner之前

Plash 1

点击Spinner后

enter image description here

Spinner代码:

public class FSSpinner extends AppCompatSpinner {

    public FSSpinner(Context context) {
        super(context);
    }

    public FSSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FSSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void
    setSelection(int position, boolean animate) {
        boolean sameSelected = position == getSelectedItemPosition();
        super.setSelection(position, animate);
        if (sameSelected) {
            OnItemSelectedListener onItemSelectedListener = getOnItemSelectedListener();
            if (onItemSelectedListener != null) {
                onItemSelectedListener.onItemSelected(this, getSelectedView(), position, getSelectedItemId());
            }
        }
    }

    @Override
    public void
    setSelection(int position) {
        boolean sameSelected = position == getSelectedItemPosition();
        super.setSelection(position);
        if (sameSelected) {
            OnItemSelectedListener onItemSelectedListener = getOnItemSelectedListener();
            if (onItemSelectedListener != null) {
                onItemSelectedListener.onItemSelected(this, getSelectedView(), position, getSelectedItemId());
            }
        }
    }
}

Seekbar xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/background"
        android:gravity="center_vertical">
        <shape
            android:shape="rectangle"
            android:tint="#77C344">
            <size android:height="30dp" />
            <solid android:color="#77C344" />
        </shape>
    </item>
    <item
        android:id="@android:id/progress"
        android:gravity="center_vertical">
        <scale android:scaleWidth="100%">
            <selector>
                <item
                    android:drawable="@android:color/transparent"
                    android:state_enabled="false" />
                <item>
                    <shape
                        android:shape="rectangle"
                        android:tint="#77C344">
                        <size android:height="30dp" />
                        <solid android:color="#77C344" />
                    </shape>
                </item>
            </selector>
        </scale>
    </item>
</layer-list>

SeekBarThumb.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:thickness="4dp"
    android:tint="#000"
    android:useLevel="false">
    <solid android:color="#000" />
    <size
        android:width="12dp"
        android:height="32dp" />
</shape>

1 个答案:

答案 0 :(得分:0)

尝试从 Seekbar.xml 中删除以下内容,此处无需提及state_enabled,因为您只有一个州

android:state_enabled="false"