我目前在将自定义 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: 这是请求的屏幕截图。 如果我不指定文本颜色,这就是它的样子,它似乎使用 Holo Dark 的默认文本颜色
手动指定 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/