如何覆盖(或隐藏)Android 库项目中定义的样式属性?
我有一组类似的应用程序。我将公共资源和代码提取到库项目中,应用程序只是覆盖它们需要的内容。在我的库中定义了以下样式:
<style name="ListItemText">
<item name="android:layout_toRightOf">@id/preview_image</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">@dimen/previewTextSize</item>
</style>
如您所见,它包含 android:layout_toRightOf
属性,因为此样式将应用于 ListView 行中的文本,该文本应显示在图像的右侧在那一行。
然而,在我的一个应用程序中,我想在图像下方显示文本。应如何定义该应用程序中的 ListItemText
样式来覆盖 android:layout_toRightOf
属性值并将其替换为 android:layout_below
?
如果我将其定义为:
<style name="ListItemText">
<item name="android:layout_below">@id/preview_image</item>
</style>
它将在图像的右侧和下方显示文本,从而有效地总结库和应用程序样式 XML 中的属性。
ps:我的问题的一种可能的解决方案是摆脱样式中的 android:layout_toRightOf
并将其移动到布局 xml 中。然后在目标应用程序中可以重新定义/覆盖该布局。但我正在寻找基于样式的解决方案,因为它可以提供更简单直接的属性覆盖方式。
(我还可以将继承的样式与 parent
属性一起使用,但这又需要在应用程序中覆盖布局,我试图避免这种情况)。
I have a set of similar applications. I extracted common resources and code to the library project and applications just override what they need to. In my library there is the following style defined:
<style name="ListItemText">
<item name="android:layout_toRightOf">@id/preview_image</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">@dimen/previewTextSize</item>
</style>
As you can see it contains android:layout_toRightOf
attribute as this style would be applied to the text in the ListView row which should be displayed to the right of the image in that row.
However in one of my applications I'd like to display the text below the image. How the ListItemText
style in that application should be defined to override android:layout_toRightOf
attribute value and replace it with android:layout_below
?
If I define it as:
<style name="ListItemText">
<item name="android:layout_below">@id/preview_image</item>
</style>
it displays text to the right and below the image, effectively summing up attributes from both library and application styles XMLs.
p.s.: One possible solution of my issue would be to get rid of android:layout_toRightOf
in the styles and move it to the layout xml instead. Then in the target application this layout can be redefined/overridden. But I'm looking for style-based solution, since it could provide more simple and straightforward way of attribute overriding.
(I can also use the inherited style with the parent
attribute, but this would again require layout overriding in the application, which I try to avoid).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要禁用
android:layout_toRightOf
,您可以将其设置为@null
。如果您的应用按如下方式定义ListItemText
样式,则它应该按您想要的方式工作:To disable
android:layout_toRightOf
, you can set it to@null
. If your app defines theListItemText
style as follows, it should work as you want:您可以使用“parent”来派生专门的样式(请参阅http://developer. android.com/guide/topics/ui/themes.html),但是用样式定义这样的布局并没有让我觉得是解决这个问题的方法。在布局文件中保留一些布局信息是可以的。
You could use "parent" to derive a specialized style (see http://developer.android.com/guide/topics/ui/themes.html) but defining the layouts like this with styles doesn't strike me as the way to carve this problem up. It's ok to leave some layout info in the layout files.