[英]How to implement Cast button as Floating Action Button?
我正在嘗試將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>
感謝任何提示。
在RelativeLayout中添加2個視圖(FAB和媒體路由按鈕)。 將兩個視圖對齊到父級中心。 最后添加“高程”屬性,以便媒體路由按鈕顯示在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.-首先,您需要添加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"
3.-作為最后一步,您只需要像這樣設置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)
}
請讓我知道這對你有沒有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.