如何更改浮动操作按钮的背景色

时间:2019-03-19 21:40:51

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

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

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let text = textField.text ?? ""
    let nsString = text as NSString
    let newText = nsString.replacingCharacters(in: range, with: string)
    return newText.count <= 8
}

然后我使用

将主题应用于按钮
<style name="scan_button">
    <item name="android:background">@color/colorPrimary</item>
</style>

这是我的activity_main.xml文件

android:theme="@style/scan_button"

1 个答案:

答案 0 :(得分:0)

要更改浮动按钮的背景颜色,请使用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"
    />

希望它对您有用。

相关问题