开发者

Android - I set a custom background for a listView, and the highlight has gone

Ok, now this is my SimpleAdapter's getView function:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View view = super.getView(position, convertView, parent);

      view.setBackgroundColor(R.drawable.color1);

      return view;
    }
}

And this is my color1.xml file, in res/drawable-lpi folder:

<?xml 开发者_StackOverflowversion="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#FFFF00FF"/> <!-- pressed -->

    <item android:state_selected="true"
          android:color="#FF0000FF"/> <!-- selected -->

    <item android:state_focused="true"
          android:color="#FF0000FF"/> <!-- focused -->  

    <item android:color="#FFFFFFFF"/> <!-- default -->
</selector>

Why i still get this?

Android - I set a custom background for a listView, and the highlight has gone


First of all, you need to know that all background settings functions of a View, actually change its background-drawable.

  • The setBackgroundColor actually creates a ColorDrawable.
  • The setBackgroundResource actually loads a drawable resource
  • The setBackgroundDrawable, obviously, uses a drawable.

When you call setBackgroundColor with your highlight.xml, you actually try to create a ColorDrawable with the value of the generated ID of R.drawable.highlight.

You actually need to call the setBackgroundResource method, which is equivalent to the "android:background" XML tag.

Your highlight.xml is actually a state-list, which is an instance of StateListDrawable. If you wanted to only change the colors, you could use a color-state-list.


You are using setBackgroundColor() which takes an integer color value... you need to use setBackgroundResource().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜