listitem中的错误样式,不使用正确的父级

时间:2014-06-28 16:00:16

标签: android layout xamarin

我有一个listitems列表视图,里面有徽章。

此徽章是带背景的TextView。

<TextView        
    style="@style/Badge"
    android:id="@+id/badgeTextView"
    android:paddingLeft="3dp"
    android:paddingRight="3dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true" />

该样式具有决定颜色的父样式/主题。

<style name="Badge" parent="Theme_X">
   <item name="android:background">@color/c_blue</item>
   <item name="android:textColor">@color/c_white</item>
</style>

<style name="Badge" parent="Theme_Y">
   <item name="android:background">@color/c_black</item>
   <item name="android:textColor">@color/c_white</item>
</style>

但是使用了上一个徽章样式的颜色。 我在布局中有其他部分的样式,但这些没有这个问题。

这是不是因为它是一个列表项,可能不会继承主题,如果是,我该如何解决这个问题。或者这是由其他原因引起的?

1 个答案:

答案 0 :(得分:0)

我认为主题/风格不会像这样工作。 我通过使用保持颜色的属性来修复它:

<attr name="badgecolor" format="reference|color" />
<attr name="badgetextcolor" format="reference|color" />

在主题样式中设置这些属性。

<style name="Theme_X" parent="Theme_Main">
    <item name="badgecolor">@color/c_black</item>
    <item name="badgetextcolor">@color/c_white</item>
</style>

<style name="Theme_Y" parent="Theme_Main">
    <item name="badgecolor">@color/c_white</item>
    <item name="badgetextcolor">@color/c_black</item>
</style>

然后只有一种带有可变颜色的徽章样式:

<style name="Badge">
    <item name="android:background">?badgecolor</item>
    <item name="android:textColor">?badgetextcolor</item>    
</style>

可以在axml文件中使用:

<TextView
    style="@style/Badge" 
    android:id="@+id/badgeTextView"
    android:paddingLeft="3dp"
    android:paddingRight="3dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true" />
相关问题