java - set background for itemView programmatically
问题描述
I'm trying to set the background of a ViewHolder
Holder.itemView
by holder.itemView.setBackground('someKindofDrawable')
.
I used gradient to create it but it doesn't give the expected result. i need to create a simple lightweight component for the background.
this is the code i use:
GradientDrawable gradientRectangle = new GradientDrawable(
Orientation.TOP_BOTTOM,
new int[]{0xFFFFFFFF,0x00000000});
gradientRectangle.setShape(GradientDrawable.RECTANGLE);
holder.itemView.setBackground(gradientRectangle);
the image is the expected result (the boarders represent the rectangle).
解决方案
我找到了答案(如果它对任何人有帮助):
public class DrawableItemSelectedBackgroundITopColor
{
LayerDrawable layerdrawable;
Drawable[] DrawableArray;
ColorDrawable topColor;
ColorDrawable bottomColor;
public DrawableItemSelectedBackgroundITopColor(Context context, String Theme)
{
topColor = new ColorDrawable(ContextCompat.getColor(context,R.color.tiny_prints_blue));
bottomColor = new ColorDrawable(ContextCompat.getColor(context,R.color.white));
DrawableArray = new Drawable[]{
topColor,
bottomColor
};
layerdrawable = new LayerDrawable(DrawableArray);
layerdrawable.setLayerInset(0,0,0,0,0);
layerdrawable.setLayerInset(1,0,9,0,0);
}
public LayerDrawable getDrawableBackgroundItemTop(){
return layerdrawable;
}
}
推荐阅读
- excel - 跨多个列的索引匹配
- python-3.x - 用于将熊猫数据框转换为嵌套 json 的数据框查询
- python - 如果超过时间限制,Python跳过循环行并打印跳过的行
- python - numpy 尝试分配比 Windows 中可用的更多的内存
- arrays - foreach中与对象数组的多重比较
- flutter - 在颤动中检测sliding_up_panel的移动
- java - 使用 JDBC 连接到 AWS RDS 实例
- javascript - 如何从数组中删除特定索引,然后在同一索引上插入一个新索引?
- python - Python,遍历 JSON 文件并跳过空白文件
- bash - 条件目的不明确