如何将Cast按钮实现为浮动操作按钮?

4

我正在尝试将Cast按钮实现为浮动操作按钮,但我不知道该如何实现。

到目前为止,我已经在工具栏上使其正常运行,但我想尝试将其作为浮动操作按钮。

我尝试了以下方法,但它并不能像真正的浮动操作按钮那样工作。

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways|snap" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:contentInsetStart="0dp"
        android:contentInsetLeft="0dp"
        android:contentInsetEnd="0dp"
        android:contentInsetRight="0dp"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStart="0dp"
        app:hideOnScroll="true"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="5">

            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/ib_monitors"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:tint="@color/colorAccent"
                app:srcCompat="@drawable/ic_photo_camera_black_24dp" />

            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/ib_videos"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:tint="@color/colorAccent"
                app:srcCompat="@drawable/ic_video_library_black_24dp" />

            <androidx.appcompat.widget.AppCompatImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackgroundBorderless" />

            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/ib_settings"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:tint="@color/colorAccent"
                app:srcCompat="@drawable/ic_settings_black_24dp" />

            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/ib_account"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:tint="@color/colorAccent"
                app:srcCompat="@drawable/ic_account_circle_black_24dp" />

        </LinearLayout>

    </com.google.android.material.bottomappbar.BottomAppBar>

    <androidx.mediarouter.app.MediaRouteButton
        android:id="@+id/media_route_button"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:backgroundTint="@color/fabBackgroundTint"
        app:layout_anchor="@id/bar"
        app:tint="@color/colorAccent"/>

    <!--<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="@color/fabBackgroundTint"
        app:layout_anchor="@id/bar"
        app:srcCompat="@drawable/ic_add_a_photo_black_24dp"
        app:tint="@color/colorAccent" />-->

</androidx.coordinatorlayout.widget.CoordinatorLayout>

感谢任何技巧。

2个回答

1

将2个视图(FAB和媒体路由按钮)添加到RelativeLayout中。将两个视图都居中对齐在父视图中。最后添加“elevation”属性,以便媒体路由按钮显示在FAB的顶部:

   <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/viewpager"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="15dp">

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />

        <android.support.v7.app.MediaRouteButton
            android:id="@+id/home_media_route_button"
            android:layout_width="wrap_content"
            android:elevation="7dp"
            android:layout_height="wrap_content"
            android:mediaRouteTypes="user"
            android:visibility="visible"
            android:layout_centerInParent="true"/>
    </RelativeLayout>

1
这是我拥有的实现方式。它对我有效,希望也对你有效。 1. 首先,你需要添加MediaRouteButton。
<androidx.mediarouter.app.MediaRouteButton
        android:id="@+id/home_media_route_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:mediaRouteTypes="user"
        android:visibility="visible"/>

2.-现在,您只需将浮动按钮样式添加到按钮即可。

style="@style/Widget.Design.FloatingActionButton"

作为最后一步,您只需要像这样设置您的MediaRouteButton
 CastButtonFactory.setUpMediaRouteButton(this,home_media_route_button)

奖励

如果您希望按钮仅在可用的投放设备时显示,则将此代码添加到您的onCreate方法中。

private lateinit var castContext: CastContext

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        castContext = CastContext.getSharedInstance(this)
        castContext.addCastStateListener{state->
            if(state==CastState.NO_DEVICES_AVAILABLE) {
                home_media_route_button.visibility = View.GONE
            }else{
                home_media_route_button.visibility=View.VISIBLE
            }
        }
        if(castContext.castState==CastState.NO_DEVICES_AVAILABLE){
            home_media_route_button.visibility = View.GONE
        }else{
            home_media_route_button.visibility=View.VISIBLE
        }
        CastButtonFactory.setUpMediaRouteButton(this,home_media_route_button)
}

如果这对你有效,请让我知道。


非常感谢。我已经寻找了一个小时相同的解决方案。这个对我起作用了。 - Kaunain

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