【问题标题】:Material FAB morphing材料 FAB 变形
【发布时间】:2018-12-18 16:09:28
【问题描述】:

根据material.io,浮动操作按钮可以变形为this等操作菜单。有没有办法只使用材料库(没有第三方库)?

我已经尝试过this 库,但它在菜单关闭后根据底部应用栏打破了工厂位置。

这是我得到的结果

代码:

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()
    }
}

【问题讨论】:

    标签: android material-design floating-action-button


    【解决方案1】:

    刚刚找到解决方案: 使用com.google.android.material.transformation.FabTransformationSheetBehavior 进行工作表布局,使用com.google.android.material.transformation.FabTransformationScrimBehavior 进行叠加视图。要支持漂亮的动画,请使用 com.google.android.material.transformation.TransformationChildCardcom.google.android.material.transformation.TransformationChildLayout 作为工作表的根视图。将 fab 转换为 sheet set isExpanded to fab 到 true 并将其向后转换 set isExpanded to false

    【讨论】:

    猜你喜欢
    • 2020-02-17
    • 2015-08-23
    • 2018-02-05
    • 2022-12-05
    • 1970-01-01
    • 2016-08-15
    • 2018-11-28
    • 1970-01-01
    相关资源
    最近更新 更多