首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将前台设置为以编程方式查看

将前台设置为以编程方式查看
EN

Stack Overflow用户
提问于 2019-07-04 16:39:09
回答 2查看 3.1K关注 0票数 0

我已经以编程的方式设置了background.Now,我必须给我的LinearLayout添加一个连锁反应,所以我不仅需要设置背景,还需要设置前景。

代码:

代码语言:javascript
运行
复制
        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);
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2019-07-04 17:05:11

您可以从该属性资源中获得如下所示的可提取性:

代码语言:javascript
运行
复制
public Drawable GetDrawableFromAttrRes(int attrRes, Context context)
{
    TypedArray a = context.ObtainStyledAttributes(new int[] { attrRes });
    try
    {
        return a.GetDrawable(0);
    }
    finally
    {
        a.Recycle();
    }
}

然后,您将按以下方式使用:

代码语言:javascript
运行
复制
vh.Layout.Foreground = GetDrawableFromAttrRes(Resource.Attribute.selectableItemBackground, context);
票数 2
EN

Stack Overflow用户

发布于 2019-07-05 01:51:05

如果您有前景波纹的xml:

例如,ripple_foreground.xml(in Resources/drawable-v21):

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
     android:color="#f5f5f5">
</ripple>

你可以这样设置:

代码语言:javascript
运行
复制
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 = vh.ItemView.Context.GetDrawable(Resource.Drawable.ripple_foreground);
        vh.StaffTv.Text = items[position].Name;
    }

并需要设置LinearLayout属性。

代码语言:javascript
运行
复制
android:clickable="true"

这样的效果:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56891802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档