Drawable.SetTintList()的行为与View.BackgroundTintList(setBackgroundTintList())不同[Xamarin.Android]

时间:2017-11-01 17:58:03

标签: android android-layout xamarin.android

如果以下onDestroyView tinted_tv 使用TextView选项1 )着色,则属性SetTintList()仍为< em> null (即使在发布的runnable UI中进行评估)。

但是,当使用BackgroundTintListBackgroundTintList)(选项2 )着色时,它(setBackgroundTintList())不会。

这两个选项都按预期工作,所以我不确定它们之间的区别是什么,或者更好用的是什么?

布局

getBackgroundTintList()

代码

<TextView
    android:id="@+id/tinted_tv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"    
    android:background="@color/white"
/>

根据Android.Views.View.BackgroundTintList Property文档

  

获取方法文档 [Android文档]
  如果指定,则返回应用于背景drawable的色调。

     

设置方法文档 [Android文档]
  对背景drawable应用色调。不修改当前的色调模式,默认为PorterDuff + Mode。对View.Background的后续调用将自动改变drawable并使用Drawable.SetTintList(ColorStateList)应用指定的色调和色调模式。

我认为Xamarin TextView tv = v.FindViewById<TextView>(Resource.Id.tinted_tv); // option 1 tv.Background.SetTintList(Context.Resources.GetColorStateList(Resource.Color.color_state_list)); // option 2 tv.BackgroundTintList = Context.Resources.GetColorStateList(Resource.Color.color_state_list); 属性getter / setter对应于Android的BackgroundTintList / get setBackgroundTintList()方法。 &#34; raw&#34; Android展示了同样的行为(View在调用getBackgroundTintList()后返回 null

1 个答案:

答案 0 :(得分:3)

View.BackgroundTintListView管理,当您调用它时,色调将应用于View.Background drawable。这是一个抽象层。

当您直接与Drawable.TintList合作时,没有人知道或关心它。这就是View.BackgroundTintList值不受影响的原因。

View.BackgroundTintList优先于View.Background.SetTintList。无论你怎么称呼最后胜利。

第二眼看,一个差异,最好由View来源的代码段描述:

private void applyBackgroundTint() {
    if (mBackground != null && mBackgroundTint != null) {
        final TintInfo tintInfo = mBackgroundTint;
        if (tintInfo.mHasTintList || tintInfo.mHasTintMode) {
            mBackground = mBackground.mutate();

            if (tintInfo.mHasTintList) {
                mBackground.setTintList(tintInfo.mTintList);
            }

            if (tintInfo.mHasTintMode) {
                mBackground.setTintMode(tintInfo.mTintMode);
            }

            // The drawable (or one of its children) may not have been
            // stateful before applying the tint, so let's try again.
            if (mBackground.isStateful()) {
                mBackground.setState(getDrawableState());
            }
        }
    }
}

当您设置View.BackgroundTintList时,背景可绘制状态会更新为视图状态,这就是您在处理视图时应该选择此方法的原因。

当视图状态发生变化时,无论您选择哪种方法,都会更新背景可绘制状态。

View.BackgroundTintList之上还有View.BackgroundTintMode,它反映了可绘制的API。这两个值都可以通过布局XML中的属性来设置,这是您无法使用独立的drawables。这种方法实际上可以由平台小部件使用。