开发者

changing background color of ListView disables highlight color

I've got a list of search results which are being shown in a ListActivity. When开发者_运维知识库 I change the background color of the ListActivity items, by changing the background color of the first LinearLayout (parent), the OnClick highlight stops working. Why is this and how can I make this highlight still work when I change the background color of my items? On a side note, is it possible to change the color of this highlight? Thanks.

My XML is something like this:

<LinearLayout>
<LinearLayout>
 <ImageView>
 <LinearLayout>
  <TextView>
  </TextView>
  <TextView>
  </TextView>
  <TextView>
  </TextView>
 </LinearLayout>
</LinearLayout>

and my activity is like this:

public class ResultsPage extends ListActivity {


As James says, if you change your background color in your code, when the user select an item in the ListView at execution time, it doesn't highlights the item.

You can fix these behavior, using the attribute drawSelectorOnTop="true" in your ListView definition. For example

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="true"
/>


This happens because of the way that Android handles backgrounds. The default background for a ListView is a selector drawable defined in XML which specifies different backgrounds for different states. When you tell a ListView to have a specific background image or color then you've lost the selector that told it what to do in different states.

See the section 'State List' here http://developer.android.com/guide/topics/resources/drawable-resource.html

And, to answer your second question, yes. If you want to change the highlight background just do so as specified in the documentation on that page above. You can define your own drawables as state lists with no problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜