我创建了一个包含微调器的布局并注入到我的操作栏的自定义视图中。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/navigationSpinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/actionbar_spinner_background" />
</RelativeLayout>
我希望 actionbar_spinner_background 成为具有两种状态的选择器 - 在微调器打开和关闭时有效 - 但我找不到任何检测方法。我已经经历了每个状态(state_focused 等),发现只有以下似乎有效(因为下拉菜单是一个弹出窗口),但问题是当任何东西覆盖我的应用程序时,微调器会改变状态,因为例如当状态栏被下拉时。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:drawable="@drawable/actionbar_spinner_background_up" />
<item android:drawable="@drawable/actionbar_spinner_background_down" />
</selector>
我在这里和这里也看到过类似的 stackoverflow 问题,但它们都指的是“android:state_dropdown_showing”,它似乎已经很久没有可用了(顺便说一句,我的最低 api 级别是 14),而且here,这可能是一个稍微不同的用例,但无论如何都没有答案。
目前我似乎能想出的最佳计划是覆盖我的活动的 onWindowFocusChanged() 事件,检查下拉菜单窗口/布局是否存在,然后调用 setActivated() (或类似的东西设置状态)在微调器视图上。