问题描述
我使用的是ExpandableList通过ExpandableListViewActivity。
现在,我想改变每个ChildView内有一定的TextView的文本颜色。我不想让他们全部是相同的,而不是像在OK绿色和红色等错误。
我没穿过getExpandableListAdapter()绊倒。getChildView(...),但我不能确定最后的参数(查看convertView,ViewGroup中父母)应该是。我也不太知道,如果我需要返回值(视图)投进去的东西吗?
TextView的电视=(TextView中)this.getExpandableListAdapter()getChildView(0,0,假,???,???)findViewById(R.id.xlist_child_tv)。
亲切的问候,
水母
解决方案汇总
SimpleExpandableListAdapter expListAdapter =新SimpleExpandableListAdapter(... ...参数){
@覆盖
公共查看getChildView(INT groupPosition,诠释childPosition,布尔isLastChild,查看convertView,父母的ViewGroup)
{
最后查看的itemRenderer = super.getChildView(groupPosition,childPosition,isLastChild,convertView,父母);
最终的TextView电视=(TextView中)itemRenderer.findViewById(R.id.kd_child_value); 如果(tv.getText()。的toString()。contentEquals(的getString(R.string.true)))
{
tv.setTextColor(getResources()的getColor(R.color.myred));
}
否则,如果(tv.getText()。的toString()。contentEquals(的getString(R.string.false)))
{
tv.setTextColor(getResources()的getColor(R.color.mygreen));
}
其他
{
tv.setTextColor(getResources()的getColor(R.color.white));
} 返回的itemRenderer; }
};
颜色资源:
<资源>
<颜色名称=白色>#FFFFFFFF< /彩色>
<颜色名称=myred>#FFFF3523< /彩色>
<颜色名称=mygreen>#FFA2CD5A< /彩色>
< /资源>
您应该重写你的 ExpandableListAdapter $ C $的
getChildView
方法C>的实施,并在它的内部设置的基础上,以该适配器可访问的任何标志的文本颜色。
如果变化是明确的,不要忘了叫 notifyDatasetChanged()
适配器上改变后旗!
要重写 getChildView
的方法 SimpleExpandableListAdapter
,你需要(此处匿名)扩展它:
最后SimpleExpandableListAdapter SA =新SimpleExpandableListAdapter(/ *你的参数列表* /)
{
@覆盖
公共查看getChildView(INT groupPosition,诠释childPosition,
布尔isLastChild,查看convertView,父母的ViewGroup)
{
最后查看的itemRenderer = super.getChildView(groupPosition,
childPosition,isLastChild,convertView,父母);
最终的TextView电视=(TextView中)itemRenderer.findViewById(R.id.text);
如果(/ *检查是否在groupPosition数据:childPosition是正确的* /)
tv.setTextColor(getResources()的getColor((R.color.green));
其他
tv.setTextColor(getResources()的getColor((R.color.red));
返回的itemRenderer;
}
};
更新
在着色问题就出在你的资源文件,你需要一个文本颜色设定Alpha值了,所以你应该是火红的#FFFF3523
,和你的绿色:#FFA2CD5A
:
<资源>
<颜色名称=白色>#FFFFFFFF< /彩色>
<颜色名称=myred>#FFFF3523< /彩色>
<颜色名称=mygreen>#FFA2CD5A< /彩色>
< /资源>
I am using an ExpandableList via ExpandableListViewActivity.
Now, I would like to change the text colour of a certain TextView inside each ChildView. I don't want them all to be the same, rather something like "ok" in green and "error" in red etc.
I did stumble across getExpandableListAdapter().getChildView(...), but I'm unsure what the last parameters (View convertView, ViewGroup parent) are supposed to be. Also I don't quite know if I need to cast the return value (View) into something?
TextView tv = (TextView)this.getExpandableListAdapter().getChildView(0, 0, false, ???, ???).findViewById(R.id.xlist_child_tv);
Kind regards,jellyfish
Solution summary
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(...parameters...){
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
final View itemRenderer = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
final TextView tv = (TextView) itemRenderer.findViewById(R.id.kd_child_value);
if (tv.getText().toString().contentEquals(getString(R.string.true)))
{
tv.setTextColor(getResources().getColor(R.color.myred));
}
else if (tv.getText().toString().contentEquals(getString(R.string.false)))
{
tv.setTextColor(getResources().getColor(R.color.mygreen));
}
else
{
tv.setTextColor(getResources().getColor(R.color.white));
}
return itemRenderer;
}
};
color resource:
<resources>
<color name="white">#ffffffff</color>
<color name="myred">#FFFF3523</color>
<color name="mygreen">#FFA2CD5A</color>
</resources>
You should override the getChildView
method of your ExpandableListAdapter
implementation, and inside of it set the text color based on any flag accessible to this adapter.
If the change is explicit, don't forget to call notifyDatasetChanged()
on the adapter after changing the flag!
To override the getChildView
method of a SimpleExpandableListAdapter
, you need to extend it (here anonymously):
final SimpleExpandableListAdapter sa = new SimpleExpandableListAdapter(/*your list of parameters*/)
{
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
final View itemRenderer = super.getChildView(groupPosition,
childPosition, isLastChild, convertView, parent);
final TextView tv = (TextView)itemRenderer.findViewById(R.id.text);
if (/*check whether the data at groupPosition:childPosition is correct*/)
tv.setTextColor(getResources().getColor((R.color.green));
else
tv.setTextColor(getResources().getColor((R.color.red));
return itemRenderer;
}
};
Update
The problem in coloring lies in your resources file, you need to set the alpha value too for a text color, so your red should be #FFFF3523
, and your green: #FFA2CD5A
:
<resources>
<color name="white">#ffffffff</color>
<color name="myred">#FFFF3523</color>
<color name="mygreen">#FFA2CD5A</color>
</resources>
这篇关于安卓访问的TextView里面ExpandableListViewActivity的儿童的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!