【问题标题】:How to change android design support library FAB Button border color?如何更改android设计支持库FAB Button边框颜色?
【发布时间】:2016-01-05 11:50:46
【问题描述】:

我想更改 fab 按钮边框颜色。 边框颜色为白色,内部颜色为黑色或透明

我希望我的按钮看起来像这样:

【问题讨论】:

  • 你可以使用VectorDrawable

标签: android floating-action-button android-design-library


【解决方案1】:

你可以在没有drawable的情况下制作圆形

  <android.support.design.widget.FloatingActionButton
        android:id="@+id/bottom_navigation_fab"
        style="@style/fab_material"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_gravity="bottom|center"
        app:borderWidth="3dp"
        android:backgroundTint="@color/mountain_meadow" // inner circle color
        android:layout_marginBottom="10dp"
        android:tint="@color/white"
        app:backgroundTint="@color/white" // border color
        app:srcCompat="@drawable/bottom_nav_star" />

输出:

【讨论】:

  • android:backgroundTint 仅适用于 Android 21+
  • 如何在代码中而不是在 xml 中动态更改 android:backgroundTintapp:backgroundTint
【解决方案2】:

fab.xmldrawable

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="2"
    android:useLevel="false" >
    <solid android:color="@android:color/transparent" />

    <stroke
        android:width="3dp"
        android:color="@android:color/white" />
</shape>

layout 中的浮动操作按钮

<android.support.design.widget.FloatingActionButton
        android:id="@+id/buttton_float"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_social_notifications"
        android:background="@drawable/fab"
        android:layout_margin="@dimen/fab_margin"
        android:layout_gravity="bottom|right"
        app:fabSize="normal"
        app:backgroundTint="@android:color/white"
        app:rippleColor="@android:color/black"
        app:borderWidth="0dp"
        app:elevation="2dp"
        app:pressedTranslationZ="12dp"/>

注意:您的 FAB 的自定义设计违反了针对 Floating Action Button 的 Google Material Design 指南

【讨论】:

  • 首先感谢您的回答!但是我想用黑色的baground而不是透明的来实现完全相同的渲染。我刚刚将“透明”更改为“黑色”,它用白色填充了我的 FAB。有人知道为什么这适用于透明色而不适用于黑色吗?
  • android.support.design.widget.FloatingActionButton中尝试app:backgroundTint="@android:color/black"
  • 我尝试将 backgroundTint 设置为黑色,将纯色设置为透明,我得到带有透明背景的黑色边框,当我将 backgroundTint 设置为黑色并将纯色设置为黑色时,我的整个按钮都会转动黑色的。然后,如果我将 backgroundTint 设置为白色并将纯色设置为黑色,我的整个按钮就会变成白色。这是不可理解的。 ://
  • 它不起作用,你的例子的结果是一切都是白色的
  • 我只有一个白色圆圈。使用 API 26 和 27
【解决方案3】:

首先创建一个 .xml 形状资源,我们将其命名为 ring.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:innerRadiusRatio="1"
            android:shape="ring"
            android:thicknessRatio="1"
            android:useLevel="false">

            <solid android:color="#FFF"/>
            <stroke
                android:width="5dp"
                android:color="#000"/>
        </shape>
    </item>
    <item>
        <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
                android:src="@drawable/ic_cast_light"/>
    </item>

</layer-list>

您必须使用厚度和内部半径属性才能使其正确,但应该这样做!位图源也只是一个填充物,你想把你的F图像放在那里。

然后在你声明你的工厂的地方,像这样引用你的戒指:

android:background="@drawable/ring"

在您的 java 代码中,执行以下操作:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setBackgroundResource(R.drawable.ring);

希望这会有所帮助!

【讨论】:

  • 据我所知,fab 按钮 android:background 不起作用,android:backgroundTint 是 fab 背景,只是颜色变化。对吗?
  • 而且,此代码不起作用 T-T。感谢您的关注。
【解决方案4】:

如果您想设置浮动按钮边框,那么您只需执行此操作。首先创建一个xml文件

fab_background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="2"
    android:useLevel="false" >

    <!--Here if you want to set transparent can set-->
    <solid android:color="@color/white" />

    <!--Here you can set your fab button border color-->
    <stroke
        android:width="3dp"
        android:color="@color/white" />
</shape>

然后在你的xml布局文件中像这样使用之后。

ma​​in_activity.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rl_content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="5dp"
        android:background="@drawable/fab_background">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_map"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:elevation="0dp"
            android:src="@android:drawable/ic_media_play"
            app:fabSize="normal"
            app:elevation="0dp"/>
    </LinearLayout>

</RelativeLayout>

【讨论】:

  • 您的代码运行良好,但预览时间不会显示在中间,它的右下角有空间。
  • 我自己解决了这个问题,感谢您的回答。
猜你喜欢
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 2015-12-15
  • 1970-01-01
  • 2020-05-15
  • 2015-08-12
  • 2017-06-04
相关资源
最近更新 更多