The ListView switch does not apply if specified in a style declaration.

This is more "Why?" than "how?" question, since I know a simple workaround. But I would like to know what logic is behind the behavior that I will tell.

I have declaraion style and some ListViews. I want to change the appearance of the separator. I used the following declaration:

<style name="Theme.MyApp.Dark" parent="@style/Theme.Sherlock"> <item name="android:dividerHeight">4px</item> <item name="android:divider">#123456</item> </style> 

To my surprise, only the height of the separator was applied. I tried specifying a color with an alpha value (# 12345678) and a gradient that is no good. ListViews are declared as follows:

 <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 

So, since you can see that nothing can override the style declaration. I can only change the delimiter when I specify it directly in the ListView declaration:

 <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list" android:divider="#123456" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 

Using my newly acquired skill of specifying colors depending on the chosen topic , I can get around this very easily - I just need to declare android:divider using the custom color attribute for each list.

But why can't I change android:divider in style, just like android:dividerHeight ? Is there a reason for this? I could not find explanations, error messages, or anything like that that would allow me to understand this behavior.

EDIT: The theme is applied globally, in the manifest file:

 <application android:icon="@drawable/icon" android:label="@string/app_name" android:name="org.my.app" android:theme="@style/Theme.MyApp.Dark"> 
+4
source share
2 answers

It can be installed from a theme, but with a little trick:

 <style name="MyTheme" parent="AppTheme"> <item name="android:listViewStyle">@style/MyListViewStyle</item> </style> <style name="MyListViewStyle" parent="@android:style/Widget.ListView"> <item name="android:divider">#F00</item> <item name="android:dividerHeight">1px</item> </style> 

Rgrds;)

+5
source

You should mention a style in Lisview like this

  <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list" style = "@style/Theme.MyApp.Dark" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 

Otherwise, this will not work.

+1
source

Source: https://habr.com/ru/post/1388026/


All Articles