FloatingActionButton例如与支持库FloatingActionButton例如与支

2019-05-10 13:07发布

最近,我读到这些职位:

Android设计支持库

Android的支持库,版本22.2.0

FloatingActionButton

但是,他们没有给我一个详细的例子有关创建新FloatingActionButton 。 所以不难理解,我问这个问题。

谁能给我一个关于它的例子吗?

任何帮助非常赞赏。 提前致谢。

编辑

我只是发现了一些问题FloatingActionButton (FAB),我想提高另一种答案。 见我的回答如下。

Answer 1:

因此,在您build.gradle文件,添加如下:

compile 'com.android.support:design:27.1.1'

AndroidX注:谷歌推出新的AndroidX扩展库 ,以取代旧的支持库。 要使用AndroidX,首先要确保您已更新gradle.properties文件 ,编辑build.gradle设置compileSdkVersion28 (或更高版本),并使用下面的行,而不是以前的compile之一。

implementation 'com.google.android.material:material:1.0.0'

接下来,在你themes.xmlstyles.xml或什么的,一定要设置这个-这是你的应用程序的口音color--和你的FAB的颜色,除非你重写它(见下图):

        <item name="colorAccent">@color/floating_action_button_color</item>

在布局的XML:

<RelativeLayout
 ...
 xmlns:app="http://schemas.android.com/apk/res-auto">

       <android.support.design.widget.FloatingActionButton
            android:id="@+id/myFAB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_plus_sign"
            app:elevation="4dp"
            ... />

</RelativeLayout>

你可以看到更多的选择文档 ( setRippleColor等),但要注意的一个是:

    app:fabSize="mini"

另一个有趣的one--改变的只是一个FAB的背景颜色,添加:

    app:backgroundTint="#FF0000"

(例如将其改变为红色)到XML的上方。

不管怎样,在代码中,活动后/片段的视图被充气....

    FloatingActionButton myFab = (FloatingActionButton)  myView.findViewById(R.id.myFAB);
    myFab.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doMyThing();
        }
    });

观察:

  • 如果您有这是一个“缝”分裂两种观点(使用RelativeLayout的,例如)有,比方说,一个负底部布局保证金边界重叠的按钮中的一个,你会发现一个问题:FAB的规模实际上是在棒棒糖与预棒棒糖很大的不同 。 实际上,你可以在AS的可视化布局编辑器中看到这个当你突然之间翻转APIs--它“喷了”,当你切换到预先棒棒糖。 究其原因,额外的大小似乎是阴影扩大在各个方向上的视图的大小。 所以,你必须考虑到这一点,当你调整FAB的利润率,如果它靠近其他的东西。
  • 这里有一个方法来删除或更改填充,如果有太多:

     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) myFab.getLayoutParams(); p.setMargins(0, 0, 0, 0); // get rid of margins since shadow area is now the margin myFab.setLayoutParams(p); } 
  • 另外,我要以编程方式放置FAB在两个区域之间的“缝隙”中的RelativeLayout通过抓住FAB的身高,除以二,并将它作为保证金抵消。 但myFab.getHeight()返回零,有人甚至膨胀后,它似乎。 相反,我用了一个ViewTreeObserver得到它的布局后,才出来,然后设置位置的高度。 看到这个提示在这里 。 它是这样的:

      ViewTreeObserver viewTreeObserver = closeButton.getViewTreeObserver(); if (viewTreeObserver.isAlive()) { viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { closeButton.getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { closeButton.getViewTreeObserver().removeOnGlobalLayoutListener(this); } // not sure the above is equivalent, but that's beside the point for this example... RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) closeButton.getLayoutParams(); params.setMargins(0, 0, 16, -closeButton.getHeight() / 2); // (int left, int top, int right, int bottom) closeButton.setLayoutParams(params); } }); } 

    不知道这是做正确的方式,但它似乎工作。

  • 看来你可以通过降低升高使按钮较小的阴影空间。
  • 如果你想在“缝”的FAB,您可以使用layout_anchorlayout_anchorGravity下面是一个例子:

     <android.support.design.widget.FloatingActionButton android:layout_height="wrap_content" android:layout_width="wrap_content" app:layout_anchor="@id/appbar" app:layout_anchorGravity="bottom|right|end" android:src="@drawable/ic_discuss" android:layout_margin="@dimen/fab_margin" android:clickable="true"/> 

记住,你可以自动拥有该按钮跳出的方式,当一个小吃吧通过在包装它出现CoordinatorLayout 。

更多:

  • 谷歌的设计支持库页
  • 该FloatingActionButton文档
  • “材料现在”从谷歌I / O 2015年谈 -支持设计库在介绍17m22s
  • 设计支持库样品/展示


Answer 2:

我刚刚发现FAB一些问题,我想,以增强另一个答案。


setRippleColor问题

所以,这个问题会来,一旦你设定的纹波颜色(上按下FAB颜色)通过编程setRippleColor 。 但是,我们还有另一种方法调用来进行设置,即:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
ColorStateList rippleColor = ContextCompat.getColorStateList(context, R.color.fab_ripple_color);
fab.setBackgroundTintList(rippleColor);

你的项目需要有这样的结构:

/res/color/fab_ripple_color.xml

而从代码fab_ripple_color.xml是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/fab_color_pressed" />
    <item android:state_focused="true" android:color="@color/fab_color_pressed" />
    <item android:color="@color/fab_color_normal"/>
</selector>

最后,改变你的FAB咯:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_action_add"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    app:fabSize="normal"
    app:borderWidth="0dp"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp"
    app:rippleColor="@android:color/transparent"/> <!-- set to transparent color -->

对于API 21或更高级别,设置的容右侧和底部24dp:

...
android:layout_marginRight="24dp"
android:layout_marginBottom="24dp" />

FloatingActionButton设计指南

正如你可以在上面我FAB XML代码看,我设置:

        ...
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        app:elevation="6dp"
        app:pressedTranslationZ="12dp"
        ...
  • 通过设置这些属性,你并不需要设置layout_marginToplayout_marginRight (仅前棒棒堂)一次。 Android将自动将其放置在屏幕的右侧咸一面,这同正常FAB在运行Android Lollipop。

      android:layout_alignParentBottom="true" android:layout_alignParentRight="true" 

或者,你可以使用这个CoordinatorLayout

        android:layout_gravity="end|bottom"
  • 你需要有6DP elevation和12dp pressedTranslationZ ,根据这个指南来自谷歌。



Answer 3:

FloatingActionButton延伸ImageView 。 所以,这是简单的像引入ImageView的布局。 下面是一个XML样本。

<android.support.design.widget.FloatingActionButton   xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/somedrawable"
    android:layout_gravity="right|bottom"
    app:borderWidth="0dp"
    app:rippleColor="#ffffff"/>

app:borderWidth="0dp"被添加为海拔问题的解决方法。



Answer 4:

对于AndroidX使用像

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/ic_add" />


文章来源: FloatingActionButton example with Support Library