android - Fragment 中的自定义 ListView 不遵守父主题

标签 android android-listview android-theme

我目前在将自定义 ListView 适配器与 Holo.Light 主题一起使用时遇到问题。在 Activity 和 fragment 中,任何 TextView 都以主题的正常颜色 (textColorPrimary) 显示。但是,自定义 ListAdapter 中的任何文本都使用默认 Holo 主题中的 textColorPrimary,有效地使文本不可读。

这是我应用程序主菜单中的一个示例:


list_main_menu.xml - ListAdapter 的行布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_height="48dip"
        android:layout_width="48dip"
        android:src="@drawable/ic_launcher"
        />

    <TextView 
        android:id="@+id/txtFirstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imgIcon"
        android:text="Line 1"
        android:textSize="12pt"
        />

    <TextView
        android:id="@+id/txtSecondLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imgIcon"
        android:layout_below="@id/txtFirstLine"
        android:text="Line 2"
        />
</RelativeLayout>

注意:我目前必须使用 android:textColor="?android:attr/textColorPrimaryInverse" 使文本可读。


fragment_main_menu.xml - 主菜单 fragment 。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical" >
    <TextView
        android:id="@+id/txtWelcome"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Username"
        android:textSize="18sp"
        />
    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />     
</LinearLayout>

AndroidManifext.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.michaeldodd.treasurehunter"
    android:versionCode="1"
    android:versionName="0.0.1" >

    <uses-sdk android:minSdkVersion="11" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name" android:name=".gui.Login" 
            android:theme="@android:style/Theme.Holo.Light">

            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".gui.Home" android:theme="@android:style/Theme.Holo.Light" />
        <activity android:name=".gui.UserProfile" android:theme="@android:style/Theme.Holo.Light" />
        <activity android:name=".gui.MapList" android:theme="@android:style/Theme.Holo.Light" /> 
    </application>
</manifest>

我目前没有使用任何自定义样式。感谢阅读,如有任何有用的评论,我们将不胜感激。

编辑 1: 这是请求的屏幕截图。 asdf 如果我不指定文本颜色,这就是它的样子,它似乎使用 Holo Dark 的默认文本颜色

asdf 手动指定 android:textColor="?android:attr/textColorPrimaryInverse" 给出了这个结果,但我对使用这样的解决方法感到不安。

最佳答案

您尚未发布任何实际代码,但由于我遇到了完全相同的问题,我猜您将错误的 Context 传递给您的自定义适配器构造函数。

我一直在做这样的事情(在我的 fragment 中):

dataSource = new MyCursorAdapter(getActivity().getApplicationContext(), R.layout.myrow, data,
            fields, new int[] { R.id.field1, R.id.field2 });

要解决此问题,我只需将 getActivity().getApplicationContext() 替换为 getActivity():

dataSource = new MyCursorAdapter(getActivity(), R.layout.myrow, data,
            fields, new int[] { R.id.field1, R.id.field2 });

它开始按预期工作。

MyCursorAdapter(扩展 SimpleCursorAdapter)构造函数:

public MyCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to) { //... }

关于android - Fragment 中的自定义 ListView 不遵守父主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8994033/

相关文章:

java - 迁移到 android studio 后,Android 主题未应用于 API v19(但应用于 API v21)

android - 如何从库项目中引用应用程序主题属性?

android - 当我尝试在 Eclipse 上安装 ADT 时出现什么问题?

android - 如何构建Android系统镜像

android - listfragment 内的多个 listview

android - 如何使用 ListAdapter 在 ListView 中打印数据

android - 如何检测 ListView 项目变为 ActiveView(离开屏幕)的时刻

android - 重新启动当前 Activity 以更改应用程序范围的主题

android - Glide 库 java.lang.NoClassDefFoundError

android - Android 应用程序中使用的声音/图像文件格式