android 如何使浮动动作按钮的背景渐变?

bt1cpqcv  于 2023-05-12  发布在  Android
关注(0)|答案(8)|浏览(127)

FAB代码:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right|end"
        android:layout_margin="16dp"
        android:src="@drawable/add_chat"
        android:tint="@android:color/white" />
</android.support.design.widget.CoordinatorLayout>

渐变背景代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="oval">
<gradient
    android:angle="90"
    android:endColor="#e4849f"
    android:startColor="#d96e30" />
</shape>

android:backgroundTint="”或app:backgroundTint="”都只使用颜色资源。有人能建议如何做到这一点吗?谢谢!

qv7cva1a

qv7cva1a1#

对于仍有此问题的用户,简单的解决方法是设置FAB前台
比如下面这个例子:

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foreground="@drawable/circle_btn_gradient"
        />

可绘制对象的代码(circle_btn_gradient.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <gradient
                android:startColor="#2F8FC5"
                android:endColor="#1FCEB9"
                android:angle="270"
                />

        </shape>
    </item>
    <item android:drawable="@drawable/baseline_add_24"
        android:top="15dp"
        android:left="15dp"
        android:right="15dp"
        android:bottom="15dp">
    </item>

</layer-list>
4ktjp1zp

4ktjp1zp2#

**第一步.**新建一个 drawable.xml,并添加此代码

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

        <item>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="oval">
                <gradient
                    android:type="linear"
                    android:angle="0"
                    android:startColor="#f6ee19"
                    android:endColor="#115ede" />
            </shape>

        </item>
        <item android:gravity="center"
            >
            <bitmap android:src="@drawable/your_icon"
                android:gravity="fill_horizontal" />

        </item>

    </layer-list>

步骤2.)现在在你的dimens.xml中添加这行代码

<dimen name="design_fab_image_size" tools:override="true">56dp</dimen>

步骤3.)最后使用*android:src="@drawable/drawable.xml"*在FAB中设置这个drawable.xml

P.S. -确保your_iconPNG,JPEG

jexiocij

jexiocij3#

接受的答案工作正常,但在添加下面的行后,它拉伸了在FAB上添加的图标

<dimen name="design_fab_image_size" tools:override="true">56dp</dimen>

我分享截图:
上一个FAB按钮

在dimen.xml中添加design_fab_image_size并添加android:src="@drawable/gradient"后,图标会被拉伸:

为了解决这个问题,我将gradient.xml替换为以下代码

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <gradient
            android:angle="90"
            android:startColor="#00FF00"
            android:endColor="#000000" />
        <size
            android:width="56dp"
            android:height="56dp" />
    </shape>
</item>
<item
    android:left="10dp"
    android:right="10dp">
    <shape android:shape="line">
        <stroke
            android:width="2dp"
            android:color="#ffffff" />
    </shape>
</item>
<item
    android:left="10dp"
    android:right="10dp">
    <rotate
        android:fromDegrees="90">
        <shape android:shape="line">
            <stroke
                android:width="2dp"
                android:color="#ffffff" />
        </shape>
    </rotate>
</item>

输出:

xpszyzbs

xpszyzbs4#

我尝试了一堆不同的答案,但都有自己的缺点或不与material FAB工作。当我意识到这不是它的目的时,我简单地用ImageView替换了它。这工作得很好,看起来一模一样。

<ImageButton
    android:id="@+id/floatingActionButton"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:background="@drawable/fab_gradient"
    android:elevation="6dp"
    ... />

fab_gradient.xml:

<ripple android:color="?attr/colorControlHighlight"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <item>
                <shape android:shape="oval">
                    <gradient
                        android:type="linear"
                        android:angle="90"
                        android:startColor="@color/startColor"
                        android:endColor="@color/endColor" />
                </shape>
            </item>
            <item android:drawable="@drawable/vector_icon" android:gravity="center" />
        </layer-list>
    </item>
</ripple>
r7s23pms

r7s23pms5#

<style name="FullFABStyle">
    <item name="fabSize">normal</item>
    <item name="maxImageSize">@dimen/fab_size</item>
    <item name="fabCustomSize">@dimen/fab_size</item>
</style>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="@dimen/fab_size"
    android:layout_height="@dimen/fab_size"
    style="@style/FullFABStyle"
    app:srcCompat="@drawable/your_background" />

所以,你不需要覆盖dimen

q9rjltbz

q9rjltbz6#

这种方法也有效。在drawable中创建一个名为fab_background.xml的形状,您可以在其中进行所有您想要的定制

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval"   xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:width="150dp" android:height="150dp"/>
    <gradient android:startColor="@color/colorAccentDark" android:endColor="@color/colorAccent"/>
</shape>

在layout中创建一个名为custom_fab.xml的布局,然后使用图像视图设置backround和fab图标

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:gravity="center"
    android:background="@drawable/fab_background"
    >

    <android.appcompat.widget.AppCompatImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:src="@drawable/right" />
</LinearLayout>

然后可以使用include引用它

示例

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@drawable/background"
    android:orientation="vertical">


    <include layout="@layout/custom_fab"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="32dp"/>
</RelativeLayout>
vddsk6oq

vddsk6oq7#

floatingActionButton: FloatingActionButton(
    child: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(30),
          gradient: LinearGradient(
              begin: Alignment.bottomLeft,
              end: Alignment.topRight,
              colors: [
                Color.fromRGBO(55, 59, 68, 1),
                Color.fromRGBO(66, 134, 244, 1)
              ]),
        ),
        child: Icon(Icons.add)),
    onPressed: () => _startAddNewTransaction(context),
  ),

Here is the image of the FloatingActionButton

vnjpjtjt

vnjpjtjt8#

使用CardView代替涟漪效果:

<androidx.cardview.widget.CardView
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardCornerRadius="28dp"
    app:cardElevation="12dp"
    app:cardUseCompatPadding="false">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/fab_gradient">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/icon_svg"
            app:tint="@color/white" />

    </RelativeLayout>

</androidx.cardview.widget.CardView>

上面的答案不适用于SVG,您可能还会看到拉伸图标等。这是一个整洁的方式,我用我的应用程序根据材料设计的晶圆厂:

**提示:**如果它没有出现在您想要的图层上方,请使用elevation属性。

相关问题