一个基于android support library 简易实现的Floating Action Menu.

一个使用FloatingActionButton实现的FloatingActionMenu。

代码

  • kotlin
class FAB @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
    init {
        LayoutInflater.from(context).inflate(R.layout.fab, this, true).apply {
            orientation = LinearLayout.HORIZONTAL
            val a = context.obtainStyledAttributes(
                    attrs, R.styleable.FAB, defStyleAttr, defStyleRes)
            fabSrc = a.getDrawable(R.styleable.FAB_fab_src)
            fabLabel = a.getString(R.styleable.FAB_fab_label)
            a.recycle()
        }
    }

    var fabSrc: Drawable?
        get() = findViewById<FloatingActionButton>(R.id.fab_button)?.drawable
        set(value) {
            findViewById<FloatingActionButton>(R.id.fab_button).setImageDrawable(value)
        }
    var fabLabel: CharSequence?
        get() = findViewById<TextView>(R.id.fab_label).text
        set(value) {
            findViewById<TextView>(R.id.fab_label).apply {
                text = value
                visibility = if (value == null) View.GONE else View.VISIBLE
            }
        }
}

class FAM @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
    var famSrc: Drawable? = null
        set(value) {
            findViewById<FloatingActionButton>(R.id.fam_button)?.setImageDrawable(value)
            field = value
        }

    init {
        orientation = LinearLayout.VERTICAL
        gravity = Gravity.END
        val a = context.obtainStyledAttributes(attrs, R.styleable.FAM, defStyleAttr, defStyleRes)
        famSrc = a.getDrawable(R.styleable.FAM_fam_src)
        a.recycle()
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        addView(LayoutInflater.from(context).inflate(R.layout.fam, this, false))
        children<LinearLayout>().forEach { it.visibility = View.INVISIBLE }
        findViewById<FloatingActionButton>(R.id.fam_button)?.let { fab ->
            fab.setImageDrawable(famSrc)
            fab.setOnClickListener {
                TransitionManager.beginDelayedTransition(this, TransitionSet().apply {
                    addTransition(Fade())
                    addTransition(Slide(Gravity.BOTTOM))
                })
                if (fab.rotation == 135F) {
                    fab.animate().rotation(0F).start()
                    children<FAB>().forEach { it.visibility = View.INVISIBLE }
                } else {
                    fab.animate().rotation(135F).start()
                    children<FAB>().forEach { it.visibility = View.VISIBLE }
                }
            }
        }
    }
}
  • resource value id定义
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FAM">
        <attr name="fam_src" format="reference" />
    </declare-styleable>
    <declare-styleable name="FAB">
        <attr name="fab_src" format="reference" />
        <attr name="fab_label" format="string" />
    </declare-styleable>
</resources>
  • FloatingActionMenu 布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.FloatingActionButton xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fam_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:fabSize="normal" />
  • FloatingActionButton 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/fab_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="3dp" />
    </android.support.v7.widget.CardView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        app:fabSize="mini" />
</LinearLayout>

使用

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <io.github.yueeng.meitu.FAM
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        app:fam_src="@drawable/ic_add">

        <io.github.yueeng.meitu.FAB
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:fab_label="按钮2"
            app:fab_src="@drawable/ic_button2" />

        <io.github.yueeng.meitu.FAB
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:fab_label="按钮1"
            app:fab_src="@drawable/ic_button1" />
    </io.github.yueeng.meitu.FAM>
</android.support.design.widget.CoordinatorLayout>

应用 https://github.com/yueeng/meitu

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,827评论 2 45
  • 原创2017-04-13壹点伍亿上书房 孩子刚上小学时,带回一张98分的试卷,我很满意。可当我知道他们班上满分的孩...
    壹点伍亿上书房阅读 501评论 8 7
  • 想你 在一个转身之后 念你的执着 在一个月影未退之前 缠绵游戏里谁恋着谁 又是谁撩拨着谁的心房 想你 不知道自己的...
    田萍阅读 166评论 0 1
  • ##健健康康## **说是比较近**比较健康
    贾滨阅读 165评论 0 0
  • 很近很近的距离其实可以是很远的,远遥不可及,只需一个转身
    049e8e02d6b7阅读 116评论 0 0

友情链接更多精彩内容