TextView文本颜色

时间:2015-01-06 07:24:11

标签: android

我有两个ListViews布局。
每个ListView都包含一些TextView和其他元素 在编辑时(在Android Studio中),我看到所有带有黑色文本颜色的TextView 但是当我运行我的应用程序时,一个ListView有黑色TextView,另一个 - 白色 有什么区别?
颜色在哪里被覆盖了?


普通视图(黑色文字颜色):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/services"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/services_total"
        android:id="@+id/total"
        />

</LinearLayout>

列出项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/service_title"
            android:id="@+id/serviceItemTitle"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/service_cost"
            android:id="@+id/serviceItemCost"
            />
    </LinearLayout>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/serviceItemSelector"
        android:layout_gravity="center_vertical"
        android:checked="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"/>

</LinearLayout>

有问题的视图(白色文字颜色):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/appointments"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <!--
    This TextView here is just for testing.
    It has black color while running.
    So it seems that the problem is in ListView
    -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView4"
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

列出项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_unknown"
            android:textAppearance="?android:attr/textAppearanceLarge"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/appointment_cost"
            android:id="@+id/cost"/>
    </LinearLayout>

    <TextView
        android:id="@+id/dateTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/date_unknown"
        android:layout_gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

</LinearLayout>

UPDATE(适配器):

带有黑色TextViews的ListView适配器:

public class Services extends ArrayAdapter<Service> {

    public Services (Context context, List<Service> services) {
        super(context, 0, services);
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        Service service = getItem(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.appointment_service_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.title = convertView.findViewById(R.id.serviceItemTitle);
            viewHolder.cost = convertView.findViewById(R.id.serviceItemCost);
            viewHolder.selector = convertView.findViewById(R.id.serviceItemSelector);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ((TextView) viewHolder.title).setText(service.title);
        String cost = new DecimalFormat("#.##").format(service.price);
        ((TextView) viewHolder.cost).setText(String.format(getContext().getString(R.string.service_cost), cost));
        ((CheckBox) viewHolder.selector).setChecked(service.isSelected);

        return convertView;
    }

    private static class ViewHolder {

        View title;
        View cost;
        View selector;

    }

}

带有白色TextViews的ListView适配器:

public class History extends ArrayAdapter<Appointment> {

    public History (Context context, List<Appointment> appointments) {
        super(context, 0, appointments);
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        Appointment appointment = getItem(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.history_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.title = convertView.findViewById(R.id.title);
            viewHolder.cost = convertView.findViewById(R.id.cost);
            viewHolder.dateTime = convertView.findViewById(R.id.dateTime);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ((TextView) viewHolder.title).setText(appointment.carwash.title);
        float cost = 0;
        for (Service service : appointment.services) {
            if (service.isSelected) {
                cost += service.price;
            }
        }
        ((TextView) viewHolder.cost).setText(String.format(
            getContext().getString(R.string.appointment_cost),
            new DecimalFormat("#.##").format(cost)
        ));
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(appointment.date);
        ((TextView) viewHolder.dateTime).setText(...calendar conversions here...);

        return convertView;
    }

    private static class ViewHolder {

        View title;
        View cost;
        View dateTime;

    }

}

1 个答案:

答案 0 :(得分:1)

所以问题在于适配器&#39;上下文。 感谢@Amrola,我找到了它。

这就是我将ListViews与适配器链接的方式:

&#34;黑色&#34; ListView(&#34;此&#34;适配器取自活动):

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_appointment);

    ...

    ListView services = (ListView) findViewById(R.id.services);
    Services servicesAdapter = new Services(this, carwash.services);
    services.setAdapter(servicesAdapter);

    ...
}

&#34;白色&#34; ListView(&#34;这个&#34;适用于应用程序):

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);

    ...

    final ListView appointmentsView = (ListView) findViewById(R.id.appointments);

    /* THIS IS ASYNCHRONOUS TASK!!! */
    DB.getAppointments(new AppointmentsGetterTask.AppointmentsGetterListener() {
        @Override
        public void onAppointmentsReady (List<Appointment> appointments) {
            History history = new  History(App.getContext(), appointments);
            appointmentsView.setAdapter(history);
        }
    });
}

这是App类:

public class App extends Application {

    private static Context context;

    @Override
    public void onCreate () {
        super.onCreate();

        context = getApplicationContext();
    }

    public static Context getContext () {
        return context;
    }

}

老实说,我不知道这些背景之间有什么区别。 欢迎提出任何意见。

感谢。