[英]Android change TextView Font color in a ListView for a condition
我遇到的問題與更改由Kartheek回答的Android列表視圖中指定項目的顏色 (謝謝)並適用於測試如下:
adapter = new ArrayAdapter<String>(this,R.layout.db_msg,messaggi){
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view1 = super.getView(position, convertView, parent);
// if (position % 2 == 0) { //Place the condition where you want
to change the item color.
testo = messaggi.get(position);
if(testo.substring(0,5).equals("27-09")){
view1.setBackgroundColor(Color.parseColor("#e0e0ef"));
} else {
//Setting to default color.
view1.setBackgroundColor(Color.WHITE);
}
return view1;
}
};
問題:我寧願更改字體顏色,但view1.setTextColor(Color.parseColor(“#E0E0EF”);似乎不起作用;
看起來您錯過了命令中的最后一個結束符')'。 否則,它說明正確:
view1.setTextColor(Color.parseColor("#E0E0EF"));
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view1;
view1=convertView;
if (convertView == null) {
view1 = inflater.inflate(R.layout.db_msg, null);
testo = messaggi.get(position);
if(testo.substring(0,5).equals("27-09")){
view1.setBackgroundColor(Color.parseColor("#e0e0ef"));
}
else {
//Setting to default color.
view1.setBackgroundColor(Color.WHITE);
}
return convertView;
}
向我們展示db_msg此布局,在該布局中,只有一個TextView只是獲取了它的名稱並替換為“ tvIDFrom_db_msg_layout”
adapter = new ArrayAdapter<String>(this,R.layout.db_msg,messaggi){
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view1 = super.getView(position, convertView, parent);
if (position % 2 == 0) { //Place the condition where you want to change the item color.
testo = messaggi.get(position);
TextView tvText = (TextView) view1.findViewById(R.id.tvIDFrom_db_msg_layout);
if(testo.substring(0,5).equals("27-09")){
tvText.setTextColor(Color.parseColor("#yourHexCode"));
} else {
//Setting to default color.
tvText.setTextColor(Color.WHITE);
}
return view1;
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.