Как изменить цвет фона кнопки «Плавающее действие» - PullRequest
0 голосов
/ 20 марта 2019

У меня есть плавающая кнопка действия в моем файле 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>

1 Ответ

0 голосов
/ 20 марта 2019

Чтобы изменить цвет фона кнопки с плавающим действием, используйте атрибут 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"
    />

Надеюсь, это сработает для вас.

...