Answers:
我测试的各种组合android:background,android:backgroundTint和android:backgroundTintMode。
android:backgroundTint将颜色滤镜android:background应用于与一起使用时的资源android:backgroundTintMode。
结果如下:
如果您想进一步尝试,请参见以下代码:
<?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:background必须设置属性才能在android:backgroundTint上显示TextView。如果是Button,我猜测框架已经设置了某种背景/颜色。
由于已经涵盖了差异,因此我不会在此强调太多,但是请注意以下几点:
android:backgroundTint android:backgroundTintMode 仅在API 21上可用android:background,并且您想要更改其默认颜色,则可以使用android:backgroundTint为其添加阴影。例
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="@android:drawable/ic_dialog_email" />
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="@android:drawable/ic_dialog_email"
android:backgroundTint="@color/colorAccent" />
另一个例子
如果你试图改变的强调色FloatingActionButton用android:background你不会注意到一个变化,这是因为它已经被利用app:srcCompat,所以为了做到这一点,你可以使用android:backgroundTint,而不是
用于应用背景色的混合模式。
色调适用于背景。必须是一个颜色值,在形式
#rgb,#argb,#rrggbb,或#aarrggbb。这也可能是对包含此类型值的资源(形式为“ @ [package:] type:name”)或主题属性(形式为“?[package:] [type:] name”)的引用。
android:backgroundTint没有android:background,本次的TextView不会改变任何东西。但是,我android:backgroundTint在Button中尝试,按钮的颜色看起来与我设置的backgroundTint相同。您能解释这些情况吗?