有没有办法在一种样式中为视图组的不同元素指定不同的样式?
例如,我有一个列表,其中每个项目都有标题,详细信息和分隔符。 我希望能够在styles.xml中有一个样式标记,它将边填充应用于文本,但只有顶部/底部填充到分隔符。
我意识到我的想法可能会受到css的影响,我只是想知道是否在Android中有一个优雅的解决方案。
Is there a way to specify different styles for different elements of a view group within one style?
For example, I have a list where each item has a title, details and separator. I would like to be able to have one style tag in my styles.xml that applies side padding to the text but only top/bottom padding to the separator.
I realise my thinking could be influenced by css, I was just wondering if there was an elegant solution in android for it.
最满意答案
样式可以具有父级,结果视图应用属性“自上而下”,即子样式将覆盖冲突的父样式属性。
代码我最终使用(我有一个包含多个分隔符的列表,我只想在最后一个填充):
<style name="ListItemSeparator"> <item name="android:background">android:attr/listDivider</item> <item name="android:layout_height">1px</item> </style> <style name="LastListItemSeparator" parent="ListItemSeparator"> <item name="android:layout_marginBottom">20dp</item> </style>更多信息可以在这里找到
A style can have a parent, with the resulting view applying attributes "top-down", i.e. the child styles will override conflicting parent style attributes.
Code I ended up using (I have a list with multiple separators, I wanted padding only on the last one):
<style name="ListItemSeparator"> <item name="android:background">android:attr/listDivider</item> <item name="android:layout_height">1px</item> </style> <style name="LastListItemSeparator" parent="ListItemSeparator"> <item name="android:layout_marginBottom">20dp</item> </style>More info can be found here
更多推荐
发布评论