如何更改支持工具栏菜单项的onPress行为?

Jan*_*usz 2 android android-toolbar

我使用支持包中的新ToolBar.

在Android 5.0上,我的触控效果很好.在较低的版本我也得到一个按下状态,但它是非常微妙的.

如何更改工具栏菜单项的按下状态?

小智 6

您可以使用更改样式中的按下状态效果

    <item name="selectableItemBackground">
        @drawable/selectable_item_background_selector
    </item>

    <item name="android:selectableItemBackground">
        @drawable/selectable_item_background_selector
    </item>
Run Code Online (Sandbox Code Playgroud)

在Android 5.0上,您可以覆盖"style-v21"中的样式并设置一个ripple drawable(在drawable-v21中):

    <item name="android:selectableItemBackgroundBorderless">
        @drawable/ripple_selector
    </item>
Run Code Online (Sandbox Code Playgroud)

要仅为工具栏更改按下的状态样式,您可以设置自定义主题

    <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      app:theme="@style/ToolbarStyle"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="?attr/colorPrimary"
      android:minHeight="?attr/actionBarSize"/>
Run Code Online (Sandbox Code Playgroud)