android - 获取 TapTargetView 的 MenuItem View 引用

标签 android android-layout android-view menuitem android-menu

我正在尝试使用 TapTargetView对于菜单项,但我看不到它。

我的代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);

    new TapTargetSequence(this)
            .targets(
                    TapTarget.forView(menu.findItem(R.id.add).getActionView(), "Gonna"))

            .listener(new TapTargetSequence.Listener() {
                // This listener will tell us when interesting(tm) events happen in regards
                // to the sequence
                @Override
                public void onSequenceFinish() {
                    // Yay
                }

                @Override
                public void onSequenceStep(TapTarget lastTarget, boolean targetClicked) {

                }


                @Override
                public void onSequenceCanceled(TapTarget lastTarget) {
                    // Boo
                }
            });


    return true;
}

错误:

java.lang.IllegalArgumentException: Given null view to target

我该如何解决这个问题? 我试过将 android:actionViewClass 添加到 xml 文件,但没有成功。

最佳答案

经过反复的搜索和测试,终于找到了可行的解决方案!

只需在 onCreateOptionsMenu() 中获取对菜单项的引用。启动一个处理程序,以便在您获取 id 引用之前正确地膨胀 View 。否则您将收到空 View 错误

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu,menu);

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            final View view = findViewById(R.id.askHelp);

            TapTargetView.showFor(BasicInformation.this,                 // `this` is an Activity
                    TapTarget.forView(view, "You can tap here to get Chat Support")
                            // All options below are optional
                            .outerCircleColor(R.color.colorAccent)      // Specify a color for the outer circle
                            .outerCircleAlpha(0.96f)            // Specify the alpha amount for the outer circle
                            .targetCircleColor(R.color.white)   // Specify a color for the target circle
                            .titleTextSize(30)                  // Specify the size (in sp) of the title text
                            .titleTextColor(R.color.white)      // Specify the color of the title text
                            .textColor(R.color.white)            // Specify a color for both the title and description text
                            .textTypeface(Typeface.SANS_SERIF)  // Specify a typeface for the text
                            .dimColor(R.color.black)            // If set, will dim behind the view with 30% opacity of the given color
                            .drawShadow(true)                   // Whether to draw a drop shadow or not
                            .cancelable(true)                  // Whether tapping outside the outer circle dismisses the view
                            .tintTarget(true)                   // Whether to tint the target view's color
                            .transparentTarget(false)           // Specify whether the target is transparent (displays the content underneath)
                            .targetRadius(60),                  // Specify the target radius (in dp)
                    new TapTargetView.Listener() {          // The listener can listen for regular clicks, long clicks or cancels
                        @Override
                        public void onTargetClick(TapTargetView view) {
                            super.onTargetClick(view);      // This call is optional
                            //doSomething();
                        }
                    });

        }
    });


    return true;
}

关于android - 获取 TapTargetView 的 MenuItem View 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44514025/

相关文章:

android - 将字符串传递给 fragment

android - 以固定的时间间隔更改背景

android - 在 TableRow 中对齐 TextView

android - fragment 似乎有点矫枉过正?不可能有 MVC 架构?

android - android中的视频质量?

Android 模拟器使用了错误的布局资源

java - 将 Android 模块从库切换到应用程序会导致 "Unable to resolve dependency for :app@debug/compileClasspath"/

android - 需要 bool 类型的值 : Popup Menu

xml - 如何在我的自定义形状 xml 中添加图像

android libvlc 多面问题