一個(gè)基于android support library 簡易實(shí)現(xiàn)的Floating Action Menu.

一個(gè)使用FloatingActionButton實(shí)現(xiàn)的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>

應(yīng)用 https://github.com/yueeng/meitu

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市纵东,隨后出現(xiàn)的幾起案子参淫,更是在濱河造成了極大的恐慌拔第,老刑警劉巖义矛,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件咆槽,死亡現(xiàn)場離奇詭異,居然都是意外死亡劳淆,警方通過查閱死者的電腦和手機(jī)链沼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沛鸵,“玉大人括勺,你說我怎么就攤上這事∏” “怎么了疾捍?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長栏妖。 經(jīng)常有香客問我拾氓,道長,這世上最難降的妖魔是什么底哥? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任咙鞍,我火速辦了婚禮,結(jié)果婚禮上趾徽,老公的妹妹穿的比我還像新娘续滋。我一直安慰自己,他們只是感情好孵奶,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布疲酌。 她就那樣靜靜地躺著,像睡著了一般了袁。 火紅的嫁衣襯著肌膚如雪朗恳。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天载绿,我揣著相機(jī)與錄音粥诫,去河邊找鬼。 笑死崭庸,一個(gè)胖子當(dāng)著我的面吹牛怀浆,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播怕享,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼执赡,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了函筋?” 一聲冷哼從身側(cè)響起沙合,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎跌帐,沒想到半個(gè)月后首懈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體芳来,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年猜拾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了即舌。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,745評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡挎袜,死狀恐怖顽聂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情盯仪,我是刑警寧澤紊搪,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站全景,受9級特大地震影響耀石,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜爸黄,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一滞伟、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧炕贵,春花似錦梆奈、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至鳖轰,卻和暖如春清酥,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蕴侣。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工焰轻, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人睛蛛。 一個(gè)月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓鹦马,卻偏偏與公主長得像胧谈,于是被迫代替她去往敵國和親忆肾。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內(nèi)容

  • afinalAfinal是一個(gè)android的ioc菱肖,orm框架 https://github.com/yangf...
    passiontim閱讀 15,429評論 2 45
  • 原創(chuàng)2017-04-13壹點(diǎn)伍億上書房 孩子剛上小學(xué)時(shí)和悦,帶回一張98分的試卷,我很滿意渠缕「胨兀可當(dāng)我知道他們班上滿分的孩...
    壹點(diǎn)伍億上書房閱讀 405評論 8 7
  • 想你 在一個(gè)轉(zhuǎn)身之后 念你的執(zhí)著 在一個(gè)月影未退之前 纏綿游戲里誰戀著誰 又是誰撩撥著誰的心房 想你 不知道自己的...
    田萍閱讀 143評論 0 1
  • ##健健康康## **說是比較近**比較健康
    賈濱閱讀 144評論 0 0
  • 很近很近的距離其實(shí)可以是很遠(yuǎn)的,遠(yuǎn)遙不可及亦鳞,只需一個(gè)轉(zhuǎn)身
    049e8e02d6b7閱讀 97評論 0 0