工具栏中的Android菜单项工具提示无法正常工作

时间:2015-06-19 07:43:38

标签: android android-actionbar android-toolbar

从操作栏文档:

  

如果操作项仅显示图标,则用户可以长按该项以显示显示操作项标题的工具提示。 android:图标始终是可选的,但建议使用。

但在我的情况下,Android菜单项工具栏中的工具提示无法正常工作。

这就是我在styles.xml中的内容:

<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <!-- Actionbar color -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <!--Status bar color-->
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <!--Window color-->
    <item name="android:windowBackground">@null</item>

    <!--drawerArrowStyle-->
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

    <!--Activity enter and exit animation-->
    <item name="android:windowAnimationStyle">@style/TranslateEnterExitAnimation</item>

    <!--<item name="colorAccent">#EC9290</item>-->
    <item name="android:autoCompleteTextViewStyle">@style/CursorColor</item>
</style>

和我的工具栏:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize" />

和menu_feedback.xml

<item
    android:id="@+id/menu_item_action_send"
    android:title="@string/send_text"
    android:icon="@drawable/ic_actionbar_send"
    app:showAsAction="always"/>

我在HTC设备上得到这个,PS:Nexus 5还可以。 enter image description here

但正常的吐司是好的。 enter image description here

那么,是否有一个优雅的解决方案可以使工具提示正常工作? THX。

2 个答案:

答案 0 :(得分:0)

我不确定您是否可以轻松覆盖默认行为,但是对于解决方法,您可以创建自己的项目as hinted here,然后您就可以为自己的项目设置监听器,以及show toast under the items关于长篇新闻事件。

答案 1 :(得分:0)

最后,我解决了以下问题。

首先,使用

自定义ActionMenuItemView

final Context context = getContext().getApplicationContext();

而不是

final Context context = getContext();

显示如下的Toast。

public class ActionMenuItemView extends android.support.v7.internal.view.menu.ActionMenuItemView{
public ActionMenuItemView(Context context) {
    super(context);
}

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

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

@Override
public boolean onLongClick(View v) {
    if (hasText()) {
        // Don't show the cheat sheet for items that already show text.
        return false;
    }

    final int[] screenPos = new int[2];
    final Rect displayFrame = new Rect();
    getLocationOnScreen(screenPos);
    getWindowVisibleDisplayFrame(displayFrame);

    final Context context = getContext().getApplicationContext();
    final int width = getWidth();
    final int height = getHeight();
    final int midy = screenPos[1] + height / 2;
    int referenceX = screenPos[0] + width / 2;
    if (ViewCompat.getLayoutDirection(v) == ViewCompat.LAYOUT_DIRECTION_LTR) {
        final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        referenceX = screenWidth - referenceX; // mirror
    }
    Toast cheatSheet = Toast.makeText(context, getItemData().getTitle(), Toast.LENGTH_SHORT);
    if (midy < displayFrame.height()) {
        // Show along the top; follow action buttons
        cheatSheet.setGravity(Gravity.TOP | GravityCompat.END, referenceX, height);
    } else {
        // Show along the bottom center
        cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
    }
    cheatSheet.show();
    return false;
}}

其次,创建abc_action_menu_item_layout.xml并将其放在布局文件夹中,而不是support-v7库中的abc_action_menu_item_layout.xml。

<com.example.widget.ActionMenuItemView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:focusable="true"
    android:paddingTop="4dip"
    android:paddingBottom="4dip"
    android:paddingLeft="8dip"
    android:paddingRight="8dip"
    android:textAppearance="?attr/actionMenuTextAppearance"
    android:textColor="?attr/actionMenuTextColor"
    style="?attr/actionButtonStyle"/>

虽然我不知道ActionMenuItemView中 getContext()。getApplicationContext() getContext()之间的区别。