Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用AppCompat v21在android中创建浮动操作按钮(FAB)?_Android_Xml - Fatal编程技术网

如何使用AppCompat v21在android中创建浮动操作按钮(FAB)?

如何使用AppCompat v21在android中创建浮动操作按钮(FAB)?,android,xml,Android,Xml,我想创建一个浮动操作按钮(向列表视图添加项目),比如谷歌日历,保持与棒棒糖之前的Android版本(5.0之前)的兼容性 我创建了这个布局: 活动main_Activity.xml: <LinearLayout ... > <include layout="@layout/toolbar"/> <RelativeLayout ... > <!-- My rest of the layout -->

我想创建一个浮动操作按钮(向列表视图添加项目),比如谷歌日历,保持与棒棒糖之前的Android版本(5.0之前)的兼容性

我创建了这个布局:

活动main_Activity.xml:

<LinearLayout ... >

     <include
         layout="@layout/toolbar"/>

     <RelativeLayout ... >

     <!-- My rest of the layout -->

          <!-- Floating action button -->
          <ImageButton style="@style/AppTheme"
                     android:layout_width="60dp"
                     android:layout_height="60dp"
                     android:text="New Button"
                     android:id="@+id/button"
                     android:src="@drawable/ic_fab"
                     android:background="@drawable/fab"
                     android:layout_alignParentBottom="true"
                     android:layout_alignParentRight="true"
                     android:layout_marginBottom="24dp"
                     android:layout_marginRight="24dp"/>

     </RelativeLayout>

 </LinearLayout>

Drawable fab.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
    <solid android:color="#ffa48bc0"/>
</shape>

Style.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#ff1d79b1</item>
        <item name="colorPrimaryDark">#ff084d95</item>
    </style>
</resources>

#ff1d79b1
#ff084d95
结果类似,但没有阴影,这是材质设计的一个特点:

日历的浮动操作按钮:

我的应用程序的浮动操作按钮:

如何将阴影添加到按钮


我已经使用了属性“高程”,但不起作用。

添加填充和高程:

 android:elevation="10dp"
 android:padding="10dp"
试试看,它支持shadow,有
minSdkVersion=7
并且隐式支持
API-21的
android:elevation
属性


原始帖子是。

有很多库在你的应用程序中添加了一个FAB(浮动操作按钮),下面是我知道的几个库


所有这些库都在棒棒糖制作前的设备上受支持,至少支持api 8。我通常使用xml drawables在棒棒糖制作前的小部件上创建阴影/立面。例如,这里有一个xml绘图工具,可以在棒棒糖制作前的设备上使用它来模拟浮动动作按钮的高度

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="8px">
    <layer-list>
        <item>
            <shape android:shape="oval">
                <solid android:color="#08000000"/>
                <padding
                    android:bottom="3px"
                    android:left="3px"
                    android:right="3px"
                    android:top="3px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#09000000"/>
                <padding
                    android:bottom="2px"
                    android:left="2px"
                    android:right="2px"
                    android:top="2px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#10000000"/>
                <padding
                    android:bottom="2px"
                    android:left="2px"
                    android:right="2px"
                    android:top="2px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#11000000"/>
                <padding
                    android:bottom="1px"
                    android:left="1px"
                    android:right="1px"
                    android:top="1px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#12000000"/>
                <padding
                    android:bottom="1px"
                    android:left="1px"
                    android:right="1px"
                    android:top="1px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#13000000"/>
                <padding
                    android:bottom="1px"
                    android:left="1px"
                    android:right="1px"
                    android:top="1px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#14000000"/>
                <padding
                    android:bottom="1px"
                    android:left="1px"
                    android:right="1px"
                    android:top="1px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#15000000"/>
                <padding
                    android:bottom="1px"
                    android:left="1px"
                    android:right="1px"
                    android:top="1px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#16000000"/>
                <padding
                    android:bottom="1px"
                    android:left="1px"
                    android:right="1px"
                    android:top="1px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#17000000"/>
                <padding
                    android:bottom="1px"
                    android:left="1px"
                    android:right="1px"
                    android:top="1px"
                    />
            </shape>
        </item>
    </layer-list>
</item>
<item>
    <shape android:shape="oval">
        <solid android:color="?attr/colorPrimary"/>
    </shape>
</item>
</layer-list>

您可以选择任何颜色来代替
?attr/colorPrimary
。以下是结果的屏幕截图:


这里有一个传统的免费 它有许多自定义项,需要SDK版本9及更高版本


@Justin Pollard xml代码工作得非常好。作为补充说明,您可以使用以下xml行添加连锁反应

    <item>
    <ripple
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:colorControlHighlight" >
        <item android:id="@android:id/mask">
            <shape android:shape="oval" >
                <solid android:color="#FFBB00" />
            </shape>
        </item>
        <item>
            <shape android:shape="oval" >
                <solid android:color="@color/ColorPrimary" />
            </shape>
        </item>
    </ripple>
</item>

不再需要创建自己的晶圆厂或使用第三方库,它包含在AppCompat 22中

只需在gradle文件中添加名为design in的新支持库:

compile 'com.android.support:design:22.2.0'
…你可以走了:

<android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_happy_image" />


elevation API仅适用于棒棒糖,您是否试用过棒棒糖设备?现在可能是您接受其中一个给定答案的时候了。事实上,在我的4.4.2三星Galaxy s III上,由于AppCompat,阴影就在那里。请阅读:谢谢您分享。我害怕再次挖掘XML代码来重新创建它。我刚刚实现了它,它很棒,只有一件奇怪的事情。当你旋转手机时,阴影/高度似乎消失了。有什么想法吗?提前谢谢Justin@AmilcarAndrade这是个好问题。我在其他xml可绘制文件中也遇到过类似的情况,但实际上我是从可绘制文件本身提取位图的。为了解决这个问题,我基本上使用了drawable作为单例;设置一次,使其不再绘制。也许你可以将drawable作为一个静态变量,并将其设置为onCreate中的背景?我还添加了一个增强功能,你可以很容易地添加riple效果,下面是代码。供将来参考,这是一个避免阴影和高程的很好的例子-->如何更改背景色?默认情况下,背景色将是主题中的“colorAccent”。不过,你也可以使用floatingActionButton。设置RippleColor(颜色),不要让“ripple”这个名字吓到你,它也可以在棒棒糖制作前的设备上使用。我没有颜色重音,如果我添加了一个并将颜色设置为其他颜色,它就不起作用了-颜色会变为绿色/蓝色。我为此使用了第三方。真的帮了我很多忙。谢谢其中的一个问题是,它仍然感觉它没有完全开发。@Jjang@user1354603 from docs
此视图的背景色默认为主题的颜色重音。如果您希望在运行时更改此设置,则可以通过setBackgroundTintList(ColorStateList)进行更改。