Material FAB 变形
Material FAB morphing
根据material.io The floating action button can be morphed into action menu like this。有没有办法只使用 material 库(没有第三方库)?
我试过 this 库,但它在菜单关闭后根据底部应用栏打破了 fab 位置。
这是我得到的结果
代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
app:layout_scrollFlags="scroll|enterAlways|snap"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fabAlignmentMode="end"
app:backgroundTint="?colorPrimary"
android:layout_gravity="bottom">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"/>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:useCompatPadding="true"
app:layout_anchor="@id/bottomBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fab"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#53000000"
android:visibility="invisible"
android:id="@+id/overlay"/>
<io.codetail.widget.RevealFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<io.files.view.MenuCardView
android:id="@+id/menu"
android:visibility="invisible"
android:layout_margin="56dp"
android:layout_gravity="bottom|end"
android:layout_width="wrap_content"
app:cardUseCompatPadding="true"
app:menu="@menu/new_tab_options"
android:layout_height="wrap_content"/>
</io.codetail.widget.RevealFrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity.kt
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.konifar.fab_transformation.FabTransformation
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
overlay.setOnClickListener {
FabTransformation.with(fab)
.setOverlay(it)
.transformFrom(menu)
}
fab.setOnClickListener {
FabTransformation.with(it)
.setOverlay(overlay)
.transformTo(menu)
}
}
override fun onBackPressed() {
if(menu.visibility== View.VISIBLE){
FabTransformation.with(fab)
.setOverlay(overlay)
.transformFrom(menu)
}else super.onBackPressed()
}
}
刚刚找到解决方案:
对 sheet 布局使用 com.google.android.material.transformation.FabTransformationSheetBehavior
,对叠加视图使用 com.google.android.material.transformation.FabTransformationScrimBehavior
。要支持漂亮的动画,请使用 com.google.android.material.transformation.TransformationChildCard
或 com.google.android.material.transformation.TransformationChildLayout
作为 sheet 的根视图。将 fab 转换为 sheet 将 isExpanded
设置为 fab 为 true
并将其向后转换设置 isExpanded
为 false
根据material.io The floating action button can be morphed into action menu like this。有没有办法只使用 material 库(没有第三方库)?
我试过 this 库,但它在菜单关闭后根据底部应用栏打破了 fab 位置。
这是我得到的结果
代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
app:layout_scrollFlags="scroll|enterAlways|snap"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fabAlignmentMode="end"
app:backgroundTint="?colorPrimary"
android:layout_gravity="bottom">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"/>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:useCompatPadding="true"
app:layout_anchor="@id/bottomBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fab"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#53000000"
android:visibility="invisible"
android:id="@+id/overlay"/>
<io.codetail.widget.RevealFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<io.files.view.MenuCardView
android:id="@+id/menu"
android:visibility="invisible"
android:layout_margin="56dp"
android:layout_gravity="bottom|end"
android:layout_width="wrap_content"
app:cardUseCompatPadding="true"
app:menu="@menu/new_tab_options"
android:layout_height="wrap_content"/>
</io.codetail.widget.RevealFrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity.kt
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.konifar.fab_transformation.FabTransformation
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
overlay.setOnClickListener {
FabTransformation.with(fab)
.setOverlay(it)
.transformFrom(menu)
}
fab.setOnClickListener {
FabTransformation.with(it)
.setOverlay(overlay)
.transformTo(menu)
}
}
override fun onBackPressed() {
if(menu.visibility== View.VISIBLE){
FabTransformation.with(fab)
.setOverlay(overlay)
.transformFrom(menu)
}else super.onBackPressed()
}
}
刚刚找到解决方案:
对 sheet 布局使用 com.google.android.material.transformation.FabTransformationSheetBehavior
,对叠加视图使用 com.google.android.material.transformation.FabTransformationScrimBehavior
。要支持漂亮的动画,请使用 com.google.android.material.transformation.TransformationChildCard
或 com.google.android.material.transformation.TransformationChildLayout
作为 sheet 的根视图。将 fab 转换为 sheet 将 isExpanded
设置为 fab 为 true
并将其向后转换设置 isExpanded
为 false