在我的应用程序中,我在主屏幕中有一个 ViewPager
,其中 2 个 fragment 具有自定义 ListViews。该应用程序有不同的主题,每个主题中的文本颜色不同。
我已经在不同的主题下实现了 XML 本身的所有 UI 属性并且工作正常,除了我从代码。
问题:如何从 XMl 控制 TextColor?
我已经尝试将 3 个主题的颜色属性设置为 TextStyle
、TextAppearence
、TextColor
等。但是相同的代码没有任何效果当这些自定义 ListView 不在 ViewPager 中时正在工作。
我没有添加任何具体的代码,因为我已经尝试了很多方法。但作为示例,这是我用于设置 TextColor 的代码。
android:textColor="?attr/listItemContentTextColor"
属性在哪里,
<attr name="listItemContentTextColor" format="color" />
在主题中指定为,
<item name="listItemContentTextColor">@android:color/white</item>
有什么方法可以从 XML 本身控制它吗?
最佳答案
好吧,我找到了问题的答案。
我无法从 XML 本身控制它,但我可以动态实现它而不是硬编码。 :)
问题仅出在 ViewPager 中的那些自定义 ListView ,它们没有从 XML 中获取属性。我就是这样解决的。
在/values/attr.xml
<attr name="listFontColor" format="color" />
在每个 Theme
中,我相应地设置了 listFontColor 属性值。
<item name="listFontColor">@android:color/white</item>
我没有检查代码中的每个主题,而是获取当前主题并在我的 Adapter
代码中分配字体颜色,如下所示,
//dynamically fetching the theme's list font Color and setting it to textview
TypedArray a = ((MyActivity)context).getTheme().obtainStyledAttributes(((MyApplication)(context.getApplicationContext())).getcurrentTheme(), new int[] {R.attr.listFontColor});
int attributeResourceId = a.getResourceId(0, 0);
listTitle.setTextColor(context.getResources().getColor(attributeResourceId));
感谢 reference :)
关于android - 在 Viewpager 中自定义 ListView 文本颜色不会根据主题更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15429486/