setBackgroundDrawable for ListView in Android

后端 未结 5 912
长发绾君心
长发绾君心 2020-12-14 21:06

How do I set a drawable as the background for a list view in a class?

if (array1.size() < 8)
{
    lv1.setBackgroundDrawable(R.drawable.bgimghs2b);
}
         


        
5条回答
  •  孤街浪徒
    2020-12-14 21:27

    That's because you're not giving it a Drawable, but an ID of a drawable. Try:

    lv1.setBackgroundDrawable(getResources().getDrawable(R.drawable.bgimghs2b))
    

    If you're in an activity. If not, then you need to get a Context and call getResources() on that.

提交回复
热议问题