添加自定义边框以在运行时查看

Add custom border to view at runtime

我想在运行时为列表视图项目添加自定义边框。但是我现在的minSDK是11,我不想改...

所以我创建了两个可绘制对象:custom_borderselected_custom_border

目前,我正在使用这个导入它们:

v.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.custom_border));

但是 View.setBackground 需要 API 级别 16...是否有替代方法?

在 API 级别低于 16 的设备上,您可以使用 setBackgroundDrawable(Drawable)

例如:

Drawable drawable = ContextCompat.getDrawable(MainActivity.this, R.drawable.custom_border);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    this.setBackground(drawable);
} else {
    this.setBackgroundDrawable(drawable);
}