我已经以编程的方式设置了background.Now,我必须给我的LinearLayout添加一个连锁反应,所以我不仅需要设置背景,还需要设置前景。
代码:
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
var vh = holder as StaffViewHolder;
vh.Layout.SetBackgroundColor(position % 2 == 0 ? Color.ParseColor("#ffffff") : Color.ParseColor("#f5f5f5"));
//vh.Layout.Foreground = "?attr/selectableItemBackground";
vh.StaffTv.Text = items[position].Name;
}
class StaffViewHolder : RecyclerView.ViewHolder
{
public TextView StaffTv { get; private set; }
public LinearLayout Layout { get; private set; }
public StaffViewHolder(View view) : base(view)
{
StaffTv = view.FindViewById<TextView>(Resource.Id.StaffItemLayout_textTv);
Layout = view.FindViewById<LinearLayout>(Resource.Id.StaffItemLayout_layout);
}
}发布于 2019-07-04 17:05:11
您可以从该属性资源中获得如下所示的可提取性:
public Drawable GetDrawableFromAttrRes(int attrRes, Context context)
{
TypedArray a = context.ObtainStyledAttributes(new int[] { attrRes });
try
{
return a.GetDrawable(0);
}
finally
{
a.Recycle();
}
}然后,您将按以下方式使用:
vh.Layout.Foreground = GetDrawableFromAttrRes(Resource.Attribute.selectableItemBackground, context);https://stackoverflow.com/questions/56891802
复制相似问题