setBackgroundDrawable for ListView in Android

后端 未结 5 883
长发绾君心
长发绾君心 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:16

    (: or use

    lv1.setBackground(R.drawable.bgimghs2b);
    
    0 讨论(0)
  • 2020-12-14 21:18

    Use this: android.view.View.setBackgroundResource(int resID)

    lv1.setBackgroundResource(R.drawable.bgimghs2b);
    
    0 讨论(0)
  • 2020-12-14 21:23

    update: That method is deprecated, instead you can use:

    1. Just API 16 or above.

      setBackground(Drawable background)

    2. if you have a previous version that API 16 use:

      setBackgroundResource(int resid)

    0 讨论(0)
  • 2020-12-14 21:25

    You should use:

    Drawable background = this.getResources().getDrawable(R.drawable.yourBackgroundDrawableID);
    lv.setBackgroundDrawable(background);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题