ShowcaseView在没有ActionBar的情况下无法工作

4

如果应用程序使用主题Theme.AppCompat.Light.NoActionBar并在应用程序中使用android.support.v7.widget.Toolbar,是否有任何方法可以使用ShowcaseView?执行应用程序时,我遇到了以下堆栈跟踪。请建议...

java.lang.RuntimeException: insertShowcaseViewWithType cannot be used when the theme has no ActionBar
        at com.github.amlcurran.showcaseview.targets.AppCompatReflector.getHomeButton(AppCompatReflector.java:32)
        at com.github.amlcurran.showcaseview.targets.AppCompatReflector.getActionBarView(AppCompatReflector.java:20)
        at com.github.amlcurran.showcaseview.targets.ActionViewTarget.setUp(ActionViewTarget.java:22)
        at com.github.amlcurran.showcaseview.targets.ActionViewTarget.getPoint(ActionViewTarget.java:29)
        at com.github.amlcurran.showcaseview.ShowcaseView$1.run(ShowcaseView.java:149)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

看到了你的问题,希望以下的回答能对你有所帮助。 - Shahriar Nasim Nafi
5个回答

12

如果活动没有ActionBar(很明显,您没有菜单项),则无法创建指向菜单项的“教程步骤”。

如果您想将圆心置于活动/片段中的视图中心,请尝试此方法。

ShowcaseView.Builder res = new ShowcaseView.Builder(activity, true)
                .setTarget(target)
                .setContentTitle("title")
                .setContentText("content");

目标参数为:

Target target = new ViewTarget(view_id, activity)

使用您的代码后,我遇到了这个错误 java.lang.IllegalArgumentException: width and height must be > 0 - hash
这似乎是一个已知的问题,请参见此处https://dev59.com/VYLba4cB1Zd3GeqPir5m。请注意,您的布局已完全加载。您可能需要添加全局布局侦听器以在加载布局后启动教程。 - Juan Aguilar Guisado

11

我通过以下代码解决了类似的问题(在代码片段中):

        new ShowcaseView.Builder(getActivity())
    .setTarget( new ViewTarget( ((View) parentView.findViewById(R.id.link_to_register)) ) )
    .setContentTitle("ShowcaseView")
    .setContentText("This is highlighting the Home button")
    .hideOnTouchOutside()
    .build();

2

如果您想在溢出菜单中显示提示,则可以尝试使用ShowCaseView#setTarget(Target t)设置以下目标:

Target = new Target() {
    @Override
    public Point getPoint() {
        int[] location = new int[2];
        toolBar.getLocationInWindow(location);
        int x = location[0] + toolBar.getWidth() - toolBar.getHeight() / 2;
        int y = location[1] + toolBar.getHeight() / 2;
        return new Point(x, y);
    }
};

1
似乎 Showcase 不太适用于 Android Studio 的热部署功能(可能是由于反射使用的原因)。当我清理了项目并重新启动后(使用正确的 Showcase 构建器),它可以正常工作。

0
我得到了一个解决方案...... 适用于Appcompat工具栏的代码
toolbar = (Toolbar) findViewById(R.id.department_name);
    toolbar.setTitle(R.string.catagory_one);
    toolbar.setSubtitle(getText(R.string.lwebsite));
    toolbar.inflateMenu(R.menu.all_articles);// add this line catching menu items
    setSupportActionBar(toolbar);

然后使用您喜欢的目标视图库。我在这里使用了两个不同的库。其中一个是

 new MaterialTapTargetPrompt.Builder(CatagoryOne_HOME.this)//library link:https://github.com/sjwall/MaterialTapTargetPrompt
            .setTarget(R.id.sync)//this is yours
            .setPrimaryText("Send your first email")
            .setSecondaryText("Tap the envelope to start composing your first email")
            .show();

    ViewTarget target = new ViewTarget(toolbar.findViewById(R.id.sync));//this is yours
    new ShowcaseView.Builder(this)
            .setContentTitle("Its My Navigation Drawer")
            .setContentText("Click here and you will get options to navigate to other sections.")
            .useDecorViewAsParent() 
            .setTarget(target)
            .build();

就这样了。愉快地编程吧......


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接