无论如何要改变ExpandableListView子文本的某些部分的颜色/字体?

时间:2014-12-22 17:20:19

标签: android fonts colors expandablelistview

我想只将我的子文本的某些部分(在我的ExpandableListview中)更改为不同的颜色和/或字体。

示例:

你好,世界! (我只想以粗体显示你好)

如果可以,请有人分享。

ExpandableListadapter:

public class ExpandListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Group> groups;

    public ExpandListAdapter(Context context, ArrayList<Group> groups) {
        this.context = context;
        this.groups = groups;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<Child> chList = groups.get(groupPosition).getItems();
        return chList.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        Child child = (Child) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_item, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.country_name);
        ImageView iv = (ImageView) convertView.findViewById(R.id.flag);

        tv.setText(child.getName().toString());
        iv.setImageResource(child.getImage());

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<Child> chList = groups.get(groupPosition).getItems();
        return chList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        Group group = (Group) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.group_item, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.group_name);
        tv.setText(group.getName());
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

2 个答案:

答案 0 :(得分:0)

在没有将文本分成单独的TextViews的情况下,我不知道有任何理智的方法。

您可以拆分传入的字符串child.getName().toString().split(",")(请参阅Split documentation)并使用生成的数组填充单独的TextView,每个TextView具有不同的样式。

例如,在您现有的getChildView

TextView tv1 = (TextView) convertView.findViewById(R.id.country_name_1);
TextView tv2 = (TextView) convertView.findViewById(R.id.country_name_2);
String[] splitString = child.getName().toString().split(",");
tv1.setText(splitString[0]);
tv2.setText(splitString[1]);

你当然会添加一些错误检查,但这取决于你(确保child.getName()不是null,splitString至少是长度2所以你没有得到一个索引出界限异常等。)。

答案 1 :(得分:0)

我决定为粗体textview创建一个setter和getter,然后我将其连接到Expandablelistview的数组列表。它可能不是最有效的方式,但它解决了我的问题。