Android 如何将backgroundTint应用于API 21的视图组?

Android 如何将backgroundTint应用于API 21的视图组?,android,android-layout,Android,Android Layout,我一直在尝试对具有可绘制背景的ConstraintLayout元素应用backgroundTint。但是,不应用色调,布局与可绘制的背景色相同(仅适用于API 21;适用于API 23以上)。它也不适用于LinearLayout和GridLayout,因此我认为这可能与视图组有关。这里是元素的简化版本 <LinearLayout android:layout_height="match_parent" android:layout_wi

我一直在尝试对具有可绘制背景的ConstraintLayout元素应用backgroundTint。但是,不应用色调,布局与可绘制的背景色相同(仅适用于API 21;适用于API 23以上)。它也不适用于LinearLayout和GridLayout,因此我认为这可能与视图组有关。这里是元素的简化版本

 <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:background="@drawable/mybackground"
        android:backgroundTint="#ff0000">

        <!--Sub views here-->

 </LinearLayout>

这是可绘制的背景

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff"/>
    <corners android:radius="10dp"/>
</shape>


如何解决此问题?

我认为
LinearLayout
用于支持旧API可能有问题

<androidx.appcompat.widget.LinearLayoutCompat
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/mybackground"
    android:backgroundTint="#ff0000"
    android:orientation="vertical">

    <!--Sub views here-->

</androidx.appcompat.widget.LinearLayoutCompat>

这里您可以阅读一些关于差异的内容

我不太确定问题出在哪里,但我找到了一种适用于API 21的解决方法

view.getBackground().setColorFilter(tint_color, PorterDuff.Mode.SRC_OVER);
具有与相同的效果

view.setBackgroundTintList(ColorStateList.valueOf(tint_color));

这不管用。backgroundTint是在API 21中引入的,所以它不可能是一个兼容性问题,对吗?无论如何,谢谢你的帮助。是的,也许你就是不能使用
backgroundTint
。您应该会在Android Studio中看到一条黄色警告:
属性backgroundTint仅用于API级别21及更高级别(当前最小值为19)
,单击
alt+Enter
➡ <代码>覆盖布局中的资源-v21
。您将有两个布局,一个用于低于21的API,另一个用于21或更高的API。因此,您将能够在旧API上使用不同的布局外观。此外,根据需要,您可以尝试将
app:backgroundTint
LinearLayoutCompat