Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

android - Custom ListView in Fragment not adhering to parent theme

I'm currently having an issue with using custom ListView adapters with the Holo.Light theme. Within the activities and fragments, any TextViews are displayed in the theme's normal colours (textColorPrimary). However, any text within the custom ListAdapter uses textColorPrimary from the default Holo theme, effectively making the text unreadable.

Here is an example from my app's main menu:


list_main_menu.xml - The row layout for the 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>

Note: I'm currently having to use android:textColor="?android:attr/textColorPrimaryInverse" to make the text readable.


fragment_main_menu.xml - The Main Menu 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>

I'm not currently using any custom styles. Thanks for reading, and any useful comments will be greatly appreciated.

EDIT 1: Here's the requested screenshots. asdf This is how it looks if I don't specify the text colour, it seems to be using the default text colour for Holo Dark

asdf Manually specifying android:textColor="?android:attr/textColorPrimaryInverse" gives this result, but I'm uneasy at using a workaround like this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You haven't posted any actual code, but since I've had exactly the same problem, I would guess that you are passing the wrong Context to your custom adapter constructor.

I've been doing something like this (within my Fragment):

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

All I had to do to fix the problem, was to replace getActivity().getApplicationContext() by getActivity():

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

And it started to work as expected.

MyCursorAdapter (extending SimpleCursorAdapter) constructor:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...