【问题标题】:Android Menu item's tool-tip in the ToolBar doesn't work properly工具栏中的 Android 菜单项的工具提示无法正常工作
【发布时间】:2015-06-19 07:43:37
【问题描述】:

来自操作栏文档:

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

但在我的情况下,工具栏中的 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 没问题。

但是普通的吐司是可以的。

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

【问题讨论】:

  • @Campiador,谢谢,我重新编辑了这个问题。

标签: android android-actionbar android-toolbar


【解决方案1】:

我不确定您是否可以轻松覆盖默认行为,但至于解决方法,您可以创建自己的项目as hinted here,然后您将能够为自己的项目设置侦听器,并且show toast under the items 长按事件。

【讨论】:

  • 是的,我还找到了自定义操作视图工具提示,但我有很多带有 MenuItems 的活动,这种方式需要做很多工作,感谢 Campiador。
【解决方案2】:

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

首先,自定义使用的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 并将其放在 layout 文件夹中,而不是 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() 之间的区别,但它的工作原理就像一个魅力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 2012-12-03
    • 2016-12-11
    相关资源
    最近更新 更多