【问题标题】:What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?android布局xml中的background、backgroundTint、backgroundTintMode属性有什么区别?
【发布时间】:2015-12-04 22:02:34
【问题描述】:

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

还有什么是backgroundTintMode ??

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

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

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

    请参阅this 以清楚了解可以使用的常量值。搜索backgroundTint 属性,即可获得描述以及各种属性。

    【讨论】:

    • 链接已损坏。
    • 不,不是。你能再检查一下吗?
    【解决方案2】:

    BackgroundTint 用作颜色过滤器。

    FEFBDE 作为色调

    37AEE4 作为背景

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

    【讨论】:

      【解决方案3】:

      android:backgroundTintMode

      用于应用背景色调的混合模式。

      android:backgroundTint

      应用于背景的色调。必须是颜色值,格式为#rgb#argb#rrggbb#aarrggbb

      这也可能是对资源的引用(形式为 “@[package:]type:name”)或主题属性(形式为 "?[package:][type:]name") 包含此类型的值。

      【讨论】:

        【解决方案4】:

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

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

        结果如下:

        如果您想进一步试验,这里是代码:

        <?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>
        

        【讨论】:

        • 在您的第二个 TextView 示例中,我想知道当仅使用 android:backgroundTint 而不使用 android:background 时,第二个 TextView 不会改变任何内容。但是,我在 Button 中尝试了android:backgroundTint,按钮的颜色看起来与我设置的 backgroundTint 颜色相同。你能解释一下这些案例吗?
        • @VinhNguyen, android:background 属性必须设置为android:backgroundTintTextView 上可见。如果是Button,我猜它已经有框架设置的某种背景/颜色。
        【解决方案5】:

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

        • android:backgroundTint android:backgroundTintMode 仅适用于 API 21
        • 如果您的小部件具有由android:background 设置的 png/矢量可绘制背景,并且您想更改其默认颜色,则可以使用 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" />
        

        另一个例子

        如果您尝试使用android:background 更改FloatingActionButton 的强调色,您不会注意到更改,因为它已经使用了app:srcCompat,因此您可以使用android:backgroundTint而是

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-30
          • 1970-01-01
          • 2023-02-24
          • 2015-04-29
          • 2016-04-29
          相关资源
          最近更新 更多