如何使用背景,backgroundTint和backgroundTintMode预棒棒糖设备

问题描述:

我使用如何使用背景,backgroundTint和backgroundTintMode预棒棒糖设备

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/background_sign_up" 
android:backgroundTint="@color/colorTranslucent" 
android:backgroundTintMode="multiply" 
android:orientation="vertical" 
android:weightSum="10"> 

<color name="colorTranslucent">#dd282d50</color> 

但这部作品在21以上。我希望它在API 17和19. 我正在扩展活动。

+1

看看这个答案:https://stackoverflow.com/questions/27735890/lollipops-backgroundtint-has-no-effect-on-a-button – jelic98

我用:

linearLayout= (LinearLayout) findViewById(R.id.linearLayout); 
linearLayout.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.colorTranslucent), PorterDuff.Mode.MULTIPLY); 

和它的API 17的工作,我甚至从XML删除它,它是在API 25个工作也。谢谢。

为了规避我的背景色彩的api 21和以上,我简单地把View作为我的布局中的第一项。所以纯xml。在这个视图中,我将使用透明的颜色来操作背景,而不是色调。例如

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.HomeFragment" 
    android:id="@+id/frameLayout" 
    android:background="@drawable/planeWing"> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/semiTransparentDarkBlue"> 
    </View> 

    <TextView 
     android:id="@+id/greeting_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="32dp" 
     android:text="@string/greeting_hello" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintHorizontal_bias="0.5" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/imageView" 
     app:layout_constraintVertical_bias="0.0" /> 

</android.support.constraint.ConstraintLayout> 

我不确定您使用的乘法方法。