支持Chromecast的Netflix和YouTube应用在操作栏中显示MediaRouteButton,但Sender App Tutorial仅显示如何在布局中显示它。如何在操作栏中显示它?
答案 0 :(得分:17)
您可以将其添加到操作栏菜单中,在操作栏中显示它:
<item
android:id="@+id/action_mediaroute"
android:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
android:actionViewClass="android.support.v7.app.MediaRouteButton"
android:showAsAction="always"
android:title="@string/action_chromecast"/>
然后在onCreateOptionsMenu
中提供初始化:
public boolean onCreateOptionsMenu( Menu menu ) {
getMenuInflater().inflate( R.menu.main, menu );
MenuItem mediaRouteItem = menu.findItem( R.id.action_mediaroute );
mMediaRouteButton = (MediaRouteButton) mediaRouteItem.getActionView();
mCastContext = new CastContext( getApplicationContext() );
MediaRouteHelper.registerMinimalMediaRouteProvider( mCastContext, this );
mMediaRouter = MediaRouter.getInstance( getApplicationContext() );
mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector( MediaRouteHelper.CATEGORY_CAST );
mMediaRouteButton.setRouteSelector( mMediaRouteSelector );
mMediaRouterCallback = new MyMediaRouterCallback();
return true;
}