我想在可扩展列表中的标题(即第1级项目)中应用不同的颜色。 我怎么能这样做?
我在Android应用中使用以下类作为可扩展列表。
public class ExpandableListAdapter extends BaseExpandableListAdapter {...}
在其中,这是对getGroupView()函数的调用
@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,如下所示。
<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。
但是,我想在#ff9900颜色的列表视图中有一些标题,而在另一种颜色(例如红色)中有其他标题。
我如何使用布尔变量根据我想要的项目颜色加载不同的XML布局?
非常感谢。
答案 0 :(得分:2)
仅考虑文字颜色,可能适用以下方式:
直接在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()来为组定义两种(或更多种)不同的布局类型,如下所示:
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;
}