【问题标题】:How to Change Floating Action Button background color如何更改浮动操作按钮的背景颜色
【发布时间】:2019-03-20 12:45:24
【问题描述】:

我的 activity_main.xml 文件中有一个浮动操作按钮。但它采用 colorAccent 的背景颜色,在我的颜色文件中是黄色的。我想给它 colorPrimary,但是在设置 android:background="@color/colorPrmary" 之后,它会将具有 colorAccent 的元素的颜色更改为 colorPrimary .. 我怎么能只为按钮做呢?.. 我尝试创建一个主题在styles.xml 但它不会接受它。

<style name="scan_button">
    <item name="android:background">@color/colorPrimary</item>
</style>

然后我使用

将主题应用于按钮
android:theme="@style/scan_button"

这是我的 activity_main.xml 文件

<?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="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/linearLayout_main"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <!--custome toolbar-->
    <include layout="@layout/tool_bar" />

    <!--Wifi name and state-->
    <LinearLayout
        android:id="@+id/linear_wifi_name"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/wifi_icon_id"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.15"
            android:src="@drawable/ic_wifi_white_36dp"/>

        <TextView
            android:id="@+id/wifi_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.50"
            android:textSize="18sp"
            android:gravity="left"
            android:layout_gravity="center"
            android:text="SOHOWIFI" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.20"
            android:textSize="16sp"
            android:layout_gravity="center"
            android:gravity="right"
            android:text="Scan"/>

        <ImageView
            android:id="@+id/scan_icon"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.10"
            android:src="@drawable/ic_keyboard_arrow_right_white_36dp"/>

    </LinearLayout>

    <!--Progess bar-->
    <ProgressBar
        style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:id="@+id/progress_bar" />



    <TextView
        android:id="@+id/result_local"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:typeface="monospace"
        android:text="Local Network:"
        android:paddingLeft="5dp"
        android:textColor="@color/colorAccent"
        android:textSize="18sp"/>

    <!-- output of list local ip and public ip-->

    <ListView
        android:id="@+id/local_network"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/scan_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end|right"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_next_button"
        android:background="@color/colorPrimary"
        />
</LinearLayout>

【问题讨论】:

    标签: xml android-activity android-studio-3.0


    【解决方案1】:

    要更改浮动操作按钮的背景颜色,请使用android:backgroundTint 属性而不是android:background 属性

    如下代码:

       <android.support.design.widget.FloatingActionButton
        android:id="@+id/scan_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end|right"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_next_button"
        android:backgroundTint="@color/colorPrimary"
        />
    

    我希望它对你有用。

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      相关资源
      最近更新 更多