如何覆盖布局嵌套视图的样式?
我正在尝试覆盖自定义主题中的 TextAppearance.Medium,该主题通过应用程序清单中的相应条目应用于我的整个应用程序。由于某种原因,我指定的样式不适用于嵌套在我使用自定义布局和 SimpleAdapter 填充的 ListView 中的视图。 该样式应用于未绑定到 ListView 的 TextView 视图。
主题定义和自定义布局的示例如下。
需要说明的是,我在使用自定义布局填充列表时没有遇到问题,我的主题肯定在我的整个应用程序中应用,而且我也知道我的列表可以使用RelativeLayout 来优化项目布局。我只是在寻找为什么我的列表项的样式不正确的答案。
我是否误解了主题继承的功能,或者是否需要继承/覆盖其他特定于列表的样式?
API 级别 7 (Android 2.1)
测试使用 HTC Evo 4G 和通用 AVD 设备
主题定义
<style name="Theme" parent="android:Theme.Light.NoTitleBar">
<item name="android:textAppearanceMedium">@style/TextAppearance.Medium</item>
</style>
<style name="TextAppearance.Medium" parent="android:style/TextAppearance.Medium">
<item name="android:textColor">@android:color/black</item>
</style>
列表项布局定义
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal" android:padding="10dp">
<TextView android:layout_width="wrap_content" android:text="TextView" android:id="@+id/jobItemDateDueTextView" android:layout_height="fill_parent" android:gravity="center" android:textStyle="bold" android:layout_marginRight="15dp"></TextView>
<LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:id="@+id/jobItemHeaderTextView" android:maxLines="1" android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:id="@+id/jobItemContentTextView"></TextView>
</LinearLayout>
</LinearLayout>
I am attempting to override TextAppearance.Medium in a custom theme that is applied to my entire application with the appropriate entry in my application manifest. For some reason, the style I have specified is not applied to views nested within a ListView that I am populating using a custom layout and a SimpleAdapter. The style is applied to TextView views that are not items bound to a ListView.
Samples of the theme definition and custom layout are below.
To elucidate, I am not having problems populating the list with items utilizing the custom layout, my theme is definitely being applied throughout my application, and I am also aware that my list item layout could be optimized by using a RelativeLayout. I am simply looking for an answer as to why my list items are not being styled correctly.
Am I misunderstanding the capabilities of theme inheritance, or are there additional list-specific styles that I need to inherit/override?
API Level 7 (Android 2.1)
Testing using HTC Evo 4G, and generic AVD device
Theme Definition
<style name="Theme" parent="android:Theme.Light.NoTitleBar">
<item name="android:textAppearanceMedium">@style/TextAppearance.Medium</item>
</style>
<style name="TextAppearance.Medium" parent="android:style/TextAppearance.Medium">
<item name="android:textColor">@android:color/black</item>
</style>
List Item Layout Definition
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal" android:padding="10dp">
<TextView android:layout_width="wrap_content" android:text="TextView" android:id="@+id/jobItemDateDueTextView" android:layout_height="fill_parent" android:gravity="center" android:textStyle="bold" android:layout_marginRight="15dp"></TextView>
<LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:id="@+id/jobItemHeaderTextView" android:maxLines="1" android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:id="@+id/jobItemContentTextView"></TextView>
</LinearLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
继续上面评论线程的讨论,应用程序 Context 不会携带与您的 Activity 相同的主题信息。当您需要
Context
来生成 UI 元素时,请使用您的Activity
实例。Continuing the discussion from the comment thread above, the application
Context
will not carry the same theming information along with it that yourActivity
does. Use yourActivity
instance when you need aContext
for generating UI elements.