Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ListView style in the resource?

Tags:

android

styles

I want to set the ListView style in the Theme, so that all the ListView of the Application will inherit the style. My code looks like this:

style.xml

<!-- Light Theme of the Application -->
<style name="AppTheme.Light" parent="Theme.Sherlock.Light">
    <item name="actionBarStyle">@style/ActionBar.Green</item>
    <item name="android:absListViewStyle">@style/AbsListViewStyle.Light</item>
</style>

<style name="AbsListViewStyle.Light" parent="android:style/Widget.AbsListView">
    <item name="background">@color/background_light</item>
    <item name="android:divider">@color/light_gray</item>
    <item name="android:dividerHeight">1dp</item>
    <item name="android:listSelector">@drawable/actionbar_background_green</item>
    <item name="android:drawSelectorOnTop">false</item>
    <item name="android:cacheColorHint">#00000000</item>
</style>

I set the theme of the Activity in the AndroidManifest.xml. But it seems that the style is not applied to the ListView. Why is that?

like image 219
teejoe Avatar asked Feb 28 '14 13:02

teejoe


2 Answers

Finally I got the reason, I should use android:listViewStyle attribute instead of absListViewStyle:

style.xml

<!-- Light Theme of the Application -->
<style name="AppTheme.Light" parent="Theme.Sherlock.Light">
    <item name="actionBarStyle">@style/ActionBar.Green</item>
    <item name="android:listViewStyle">@style/ListViewStyle.Light</item>
</style>

<style name="ListViewStyle.Light" parent="android:style/Widget.ListView">
    <item name="background">@color/background_light</item>
    <item name="android:divider">@color/light_gray</item>
    <item name="android:dividerHeight">1dp</item>
    <item name="android:listSelector">@drawable/actionbar_background_green</item>
    <item name="android:drawSelectorOnTop">false</item>
    <item name="android:cacheColorHint">#00000000</item>
</style>

In this way I can apply ListView style in the Theme xml.

like image 65
teejoe Avatar answered Oct 17 '22 16:10

teejoe


You need to set it on your ListView in xml for list view add:

    style="@style/AbsListViewStyle.Light"
like image 23
Unii Avatar answered Oct 17 '22 15:10

Unii