android layout xml中的background,backgroundTint,backgroundTintMode属性有什么区别?

Sar*_*glt 101 android android-layout

在使用android布局xml时,我遇到了backgroundTint属性.我不明白是什么.

还有什么backgroundTintMode

Yog*_*ity 85

我测试了各种组合android:background,android:backgroundTintandroid: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>
Run Code Online (Sandbox Code Playgroud)


Zai*_*ain 12

我不会过多强调差异,因为它已经涵盖,但请注意以下几点:

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

例子

<Button
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:background="@android:drawable/ic_dialog_email" />
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

<Button
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:background="@android:drawable/ic_dialog_email"
    android:backgroundTint="@color/colorAccent" />
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

另一个例子

如果你试图改变的强调色FloatingActionButtonandroid:background你不会注意到一个变化,这是因为它已经被利用app:srcCompat,所以为了做到这一点,你可以使用android:backgroundTint,而不是


Sam*_*dhi 10

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

backgroundTintMode另一方面,将帮助你申请的背景色调.它必须具有恒定的值等src_over, src_in, src_atop,.

请参阅此内容以清楚了解可以使用的常量值.搜索backgroundTint属性和描述以及各种属性将可用.