android布局xml中的background,backgroundTint,backgroundTintMode属性之间有什么区别?

What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?

在使用android布局xml时,我遇到了backgroundTint attribute。 我不明白这是为了什么。

还有什么是backgroundTintMode


我测试了android:backgroundandroid:backgroundTintandroid:backgroundTintMode的各种组合。

android:backgroundTintandroid:backgroundTintMode一起使用时,会将滤色器应用于android:background的资源。

结果如下:

Tint Check

如果您想进一步尝试,请参见以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:textSize="45sp"
        android:background="#37AEE4"
        android:text="Background" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:textSize="45sp"
        android:backgroundTint="#FEFBDE"
        android:text="Background tint" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:textSize="45sp"
        android:background="#37AEE4"
        android:backgroundTint="#FEFBDE"
        android:text="Both together" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:textSize="45sp"
        android:background="#37AEE4"
        android:backgroundTint="#FEFBDE"
        android:backgroundTintMode="multiply"
        android:text="With tint mode" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:textSize="45sp"
        android:text="Without any" />
</LinearLayout>


backgroundTint属性将帮助您向背景添加色调(阴影)。您可以通过-"#rgb","#argb","#rrggbb", or"#aarrggbb".的形式提供相同的颜色值

另一方面,backgroundTintMode将帮助您应用背景色。它必须具有常量值,例如src_over, src_in, src_atop,等。

请参考此内容以清楚了解可以使用的常数值。搜索backgroundTint属性,将提供描述以及各种属性。


由于已经涵盖了差异,因此我不会在此强调太多,但是请注意以下几点:

  • android:backgroundTint android:backgroundTintMode仅在API 21上可用
  • 如果您的小部件具有android:background设置的png / vector可绘制背景,并且想要更改其默认颜色,则可以使用android:backgroundTint为其添加阴影。

1
2
3
4
<Button
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:background="@android:drawable/ic_dialog_email" />

enter image description here

1
2
3
4
5
<Button
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:background="@android:drawable/ic_dialog_email"
    android:backgroundTint="@color/colorAccent" />

enter image description here

另一个例子

如果您尝试使用android:background更改FloatingActionButton的强调色,则不会发现任何变化,这是因为它已经利用了app:srcCompat,因此可以使用android:backgroundTint代替


android:backgroundTintMode

Blending mode used to apply the background tint.

android:backgroundTint

Tint to apply to the background. Must be a color value, in the form of #rgb, #argb, #rrggbb, or #aarrggbb.

This may also be a reference to a resource (in the form
"@[package:]type:name") or theme attribute (in the form
"?[package:][type:]name") containing a value of this type.


BackgroundTint用作滤色器。

FEFBDE作为色彩
37AEE4作为背景

尝试通过注释色调/背景查看差异,并同时设置两者时检查输出。