如何在工具栏中的媒体路由器图标上使用展示视图

时间:2016-02-09 13:17:20

标签: android chromecast showcaseview

我最近在项目中添加了工具栏,方法是通过setSupportActionbar(toolbar)手动设置它。现在我正面临一个问题,比如我无法在工具栏中获得对媒体路由器按钮的视图参考,因此展示视图无法正常工作。是否有任何解决方法在媒体路线按钮中显示展示视图?

我的菜单设置如下:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/search"
        android:orderInCategory="200"
        android:title="@string/text_search"
        android:menuCategory="system"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always" />

    <item
        android:id="@+id/media_route_menu_item"
        android:orderInCategory="100"
        android:title="@string/media_route_menu_title"
        app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
        app:showAsAction="always" />
    <item
        android:id="@+id/downloads"
        android:orderInCategory="90"
        android:icon="@drawable/ic_actionbar_download"
        android:visible="false"
        app:showAsAction="ifRoom"
        android:title="Downloads"/>
    <item
        android:id="@+id/cancel"
        android:title="Cancel"
        android:visible="false"
        app:showAsAction="never"/>


</menu>

3 个答案:

答案 0 :(得分:1)

你可以试试这个适合我的代码

    Target viewTarget = new Target() {
    @Override
    public Point getPoint() {
        return new ViewTarget(toolbar.findViewById(R.id.media_route_menu_item)).getPoint();
    }
};

sv = new ShowcaseView.Builder(this)
        .setTarget(viewTarget)
        .singleShot(2)
        .setShowcaseEventListener(this)
        .build();

答案 1 :(得分:0)

你直接得到一个指向“视图”的指针:

    Menu menu = mToolbar.getMenu();
    View view = menu.findItem(R.id.media_route_menu_item).getActionView();
    if (view != null && view instanceof MediaRouteButton) {
        new ShowcaseView.Builder(this)
                .setTarget(new ViewTarget(view))
                .setContentTitle(R.string.touch_to_cast)
                .build();
    }

如果您使用CCL,则无需引入Showcase库即可获得此功能。

答案 2 :(得分:0)

custom ShowcaseView : 

- First Initialize ShowcaseConfig Object :
  ShowcaseConfig config = new ShowcaseConfig();

- If your Set Custome Text :
        config.setTypeface(tf); 

- set delay between two view
        config.setDelay(500); // half second between each showcase view

- set Content Text and Button Size :
        if (Util.isZhCN()) {
            config.setContentTextSize(24);
            config.setButtonTextSize(22);
        } else {
            config.setContentTextSize(36);
            config.setButtonTextSize(30);
        }


        MaterialShowcaseSequence sequence = new 
  MaterialShowcaseSequence(context, "sequence example");

        sequence.setConfig(config);

        MaterialShowcaseView oneShowcaseView = new MaterialShowcaseView.Builder(context)
                .setTarget(" PUT YOUR TARGEXT VIEW").setContentText(" PUT YOUR CONTENT").build();

        sequence.addSequenceItem(oneShowcaseView);
        sequence.start();


- try this work for me.