1

我创建了一个包含微调器的布局并注入到我的操作栏的自定义视图中。

<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() (或类似的东西设置状态)在微调器视图上。

4

1 回答 1

0

您应该使用您描述的解决方案或创建一个自定义微调器适配器,您可以在其中捕获打开事件或一个完全自定义的微调器,并按照您想要的方式进行所有工作......我从来没有找到 xml/selector/layout解决这个问题。我已经看到了带有 onTouch 侦听器的解决方案,但它们是一种解决方法,因为您必须考虑所有类型的事件。如果您打算做一段时间的 android,最好创建一个自定义微调器并永远使用它。

微调器本身并没有什么特别之处,您可以通过在按钮上打开对话框来模仿它...

于 2014-05-06T20:30:39.757 回答