What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?
在使用android布局xml时,我遇到了
还有什么是
我测试了
结果如下:
如果您想进一步尝试,请参见以下代码:
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> |
另一方面,
请参考此内容以清楚了解可以使用的常数值。搜索
由于已经涵盖了差异,因此我不会在此强调太多,但是请注意以下几点:
-
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" /> |
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" /> |
另一个例子
如果您尝试使用
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作为背景
尝试通过注释色调/背景查看差异,并同时设置两者时检查输出。