以编程方式将可绘制对象设置为背景

18

编辑:非常抱歉,我从您的评论中意识到我的问题不够清晰。我会发表一个新的问题。对此我感到很抱歉,谢谢您的回答。

我正在使用 Json 文件填充 ListView。

通过我的 listadapter,我可以轻松地为列表的每一行分配适当的 json 数据。这对于文本非常有效,例如:

TextView tv = (TextView)ll.findViewById(R.id.descView);   
tv.setText(i.desc);

使用上述代码,每一行都将被正确地填充为正确的JSON数据。

然而,我无法对图像执行相同的操作。 我尝试使用以下方法从我的JSON数据中设置正确的图像:

ImageView iv = (ImageView)ll.findViewById(R.id.imgView);           
iv.setBackgroundDrawable(context.getResources().getDrawable(i.img));

我猜我在参数类型上做错了什么:“setBackgroundDrawable”需要一个drawable参数,“getDrawable”需要一个int参数。我已将我的img字段的类型设置为int,但这并不起作用。

有任何想法吗?

我的列表适配器

public class adapter extends ArrayAdapter<ListItems> {

int resource;
String response;
Context context;

//Initialize adapter
public ListItemsAdapter(Context context, int resource, List<ListItems> items) {
    super(context, resource, items);
    this.resource=resource; 
}     

@Override
public View getView(int position, View convertView, ViewGroup parent)
{

    //Get the current object
    ListItems i = getItem(position);

    //Inflate the view
    if(convertView==null)
    {
        ll = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li;
        li = (LayoutInflater)getContext().getSystemService(inflater);
        li.inflate(resource, ll, true);
    }
    else
    {
        ll = (LinearLayout) convertView;
    }

    //For the message
    TextView tv = (TextView)ll.findViewById(R.id.descView);
    tv.setText(i.desc);

// For the Img
    ImageView iv = (ImageView)ll.findViewById(R.id.imgView);
    iv.setBackgroundDrawable(context.getResources().getDrawable(i.img));

    return ll;
}

我的物品类别

    public class ListItems{
int id;
int img;    
String desc;}

这是我的 JSON 文件示例:

    [{"id":10001,"img":e1,"desc":"desc1"},
    {"id":10002,"img":e2,"desc":"desc2"},
    {"id":10003,"img":e3,"desc":"desc3"}]

你可能想使用setImageResources,它以int作为参数并设置imageview的src。但问题与setBackgroundDrawable无关。此外,您的代码无法工作。ll既不包含textview也不包含imageview。 - Blackbelt
e1是什么意思?你的JSON数据中图片在哪里? - Oam
你能发布“resource”布局吗? - Blackbelt
e1、e2、e3是您想要设置的可绘制图像的名称吗? - Mehul Joisar
4个回答

57

试试这个

iv.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.img));
或者
iv.setBackgroundResource(R.drawable.img);

5
应该使用iv.setImageResource(R.drawable.img)。 - Brave
@CorayThan 如何在之前的API版本(<= 15)中实现这个功能? - AnV
@CorayThan,你找到解决旧API的方法了吗? - Samir

14

现在由于getDrawablesetBackgroundDrawable都已经过时了,你应该像这样将drawable设置为背景:

view.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable)); 

如果您的目标是最低支持版本小于16,则可以使用以下方式进行检查:

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        view.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.your_drawable));
    } else {
        view.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable));
    }

2
如果您想更改ImageView的图像/源src,有两种方法:
1. 在布局文件中使用以下代码:`android:src="@drawable/img"` 或 `app:srcCompat="@drawable/img"`
2. 通过编程使用以下代码:`imageview.setImageResource(R.drawable.img);`
如果您想更改ImageView的背景,则可以采用以下两种方法:
1. 在布局文件中使用以下代码:`android:background="@drawable/img"`
2. 通过编程使用以下代码:`selectimg.setBackgroundResource(R.drawable.circle_filled);`
这里的`imageview`是一个ImageView,`img`是一个drawable。

0

这里是新的方法

recyclerView.setBackgroundResource(R.drawable.edit_text_button_shape);

不要使用这个,它是一种旧的方法。

recyclerView.setBackgroundDrawable(this.getResources().getDrawable(edit_text_button_shape));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接