如何更改文本颜色在扩展列表一些头

如何更改文本颜色在扩展列表一些头

本文介绍了如何更改文本颜色在扩展列表一些头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想不同的颜色适用于我的扩展列表的标题(即第一级项目)。
我该怎么做?

I would like to apply different colours to the header (i.e. the 1st level items) in my Expandable List.How can I do that?

我用下面的类我在我的Andr​​oid应用程序扩展列表。

I am using the following class for my expandable list in my Android app.

public class ExpandableListAdapter extends BaseExpandableListAdapter {...}

和里面,这是调用getGroupView()函数

And inside it, this is the call to getGroupView() function

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

我定义我的XML布局,叫list_group,如下所示。
    

I defined my XML layout, called list_group, as follows.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#000000">


<TextView
    android:id="@+id/lblListHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:textSize="17dp"
    android:textColor="#ff9900" />

 </LinearLayout>

正如你所看到的,我指定的文本颜色为#FF9900。

As you can see, I specified text color to #ff9900.

不过,我想有在#FF9900颜色列表视图中一些头,并以不同的颜色(如红色)其他头。

However, I would like to have SOME headers in the list view in #ff9900 color, and other headers in a different colour (e.g. red).

我怎么会用一个布尔变量的基础上我要为我的项目的颜色以加载不同的XML布局?

How could I use a boolean variable to load a different XML layout based on which colour I want for my item?

非常感谢。

推荐答案

只考虑文本颜色,下面的方法可能是适用的:

Considering only text colour, the following way might be applicable:

改变文字颜色直 getGroupView()

    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) {
        final String headerTitle = (String) getGroup(groupPosition);
        View view = convertView;

        if (convertView == null) {
            final LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = infalInflater.inflate(R.layout.list_group, null);
        }

        final TextView lblListHeader = (TextView) view
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        if (/* Check Your flag here*/ (groupPosition & 1) == 1) {
            lblListHeader.setTextColor(mContext.getResources().getColor(android.R.color.holo_red_light));
        } else {
            lblListHeader.setTextColor(mContext.getResources().getColor(android.R.color.holo_green_light));
        }

        return view;
    }

如果您需要完全不同的布局,那么您可以使用 getGroupTypeCount()和 getGroupType()为了定义两个(或多个)不同的布局类型的团体,这样的:

If You need completely different layout, then You might use getGroupTypeCount() and getGroupType() in order to define two (or more) different layout types for groups, like the following:

    final static int GROUP_TYPE_COUNT = 2;
    final static int GROUP_TYPE_FIRST = 0;
    final static int GROUP_TYPE_SECOND = 1;

    @Override
    public int getGroupTypeCount() {
        return GROUP_TYPE_COUNT;
    }

    @Override
    public int getGroupType(final int groupPosition) {
        return ((groupPosition & 1) == 1) ? GROUP_TYPE_FIRST : GROUP_TYPE_SECOND;
    }

    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) {
        final String headerTitle = (String) getGroup(groupPosition);
        final int type = getGroupType(groupPosition);
        View view = convertView;

        if (convertView == null) {
            final LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            switch (type) {
                case GROUP_TYPE_FIRST:
                    view = infalInflater.inflate(R.layout.list_group, null);
                    break;

                case GROUP_TYPE_SECOND:
                default:
                    view = infalInflater.inflate(R.layout.list_group_1, null);
                    break;
            }
        }

        final TextView lblListHeader = (TextView) view
                .findViewById(R.id.lblListHeader);

        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return view;
    }

这篇关于如何更改文本颜色在扩展列表一些头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 18:14