Android级联布局?(Android cascading layouts?)

编程入门 行业动态 更新时间:2024-10-28 12:29:54
Android级联布局?(Android cascading layouts?)

有没有办法在一种样式中为视图组的不同元素指定不同的样式?

例如,我有一个列表,其中每个项目都有标题,详细信息和分隔符。 我希望能够在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

更多推荐

本文发布于:2023-08-06 14:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451343.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布局   级联   Android   cascading   layouts

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!