c# - 设置前景以编程方式查看
问题描述
我已经以编程方式设置背景。现在我必须在我的 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);
}
}
解决方案
您可以从该属性资源中获取可绘制对象,如下所示:
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);
推荐阅读
- reactjs - ReactJs:如何使 componentDidUpdate 循环多次
- r - Left_join 条件下
- firebase - firebase 函数 url 重写破坏 cookie
- php - 向 Symfony 动态添加规范化器
- google-cloud-platform - 谷歌云,哪个存储更好,key/value
- r - 根据 R 中的查找表添加新列
- c++ - C++:使用 std::uint_fastn_t 的缺点
- sql-server - 设计具有特定障碍的亲子关系结构的最佳方法
- javascript - http://localhost:8000/ 不会加载 App.js 中的新更改
- angular - Angular 2 Wijmo Flex 图表系列