Android 如何更改所选项目的颜色&;设置字体&;listview中的颜色
我已经使用intents创建了一个列表视图,但为了改善我的应用程序的用户体验,如何更改列表视图中所选项目的颜色我直接使用了ListActivity类,而不是Pallete中的列表视图,因此我需要直接将代码添加到活动中这是我的代码Android 如何更改所选项目的颜色&;设置字体&;listview中的颜色,android,listview,Android,Listview,我已经使用intents创建了一个列表视图,但为了改善我的应用程序的用户体验,如何更改列表视图中所选项目的颜色我直接使用了ListActivity类,而不是Pallete中的列表视图,因此我需要直接将代码添加到活动中这是我的代码 public class BreakfastListView extends ListActivity { static final String[] breakfood = { "Strawberry shake", "Egg
public class BreakfastListView extends ListActivity {
static final String[] breakfood = {
"Strawberry shake",
"Egg muffin",
"Morning sundae",
"Peanut butter pancakes w banana",
"Dippy eggs w marmite soldiers",
"Breakfast burrito",
"Ham and cheese muffin",
"Bacon, egg and cheese bagel",
"Yoghurt mashup",
"Hot cereal mashup"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, breakfood));
// Setting up the list array above
}
protected void onListItemClick(ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
String openbreakclass = breakfood[position];
if( "Strawberry shake".equals(openbreakclass))
{
Intent selectedIntent = new Intent(v.getContext(), Strawberry_Shake.class );
startActivity(selectedIntent);
} else if("Egg muffin".equals(openbreakclass))
{
Intent selectedIntent = new Intent(v.getContext(), Egg_muffin.class );
startActivity(selectedIntent);
}else if("Morning sundae".equals(openbreakclass)){
Intent selectedIntent = new Intent(v.getContext(), Morning_sundae.class );
startActivity(selectedIntent);
}else if("Peanut butter pancakes w banana".equals(openbreakclass)){
Intent selectedIntent = new Intent(v.getContext(), Peanut_butter_pancakes_w_banana.class );
startActivity(selectedIntent);
}else if("Dippy eggs w marmite soldiers".equals(openbreakclass)){
Intent selectedIntent = new Intent(v.getContext(), Dippy_eggs_w_marmite_soldiers.class );
startActivity(selectedIntent);
}else if("Breakfast burrito".equals(openbreakclass)){
Intent selectedIntent = new Intent(v.getContext(), Breakfast_burrito.class );
startActivity(selectedIntent);
}else if("Ham and cheese muffin".equals(openbreakclass)){
Intent selectedIntent = new Intent(v.getContext(), Ham_and_cheese_muffin.class );
startActivity(selectedIntent);
}else if("Bacon, egg and cheese bagel".equals(openbreakclass)){
Intent selectedIntent = new Intent(v.getContext(), Bacon_egg_and_cheese_bagel.class );
startActivity(selectedIntent);
}else if("Yoghurt mashup".equals(openbreakclass)){
Intent selectedIntent = new Intent(v.getContext(), Yoghurt_mashup.class );
startActivity(selectedIntent);
}else if("Hot cereal mashup".equals(openbreakclass)){
Intent selectedIntent = new Intent(v.getContext(), Hot_cereal_mashup.class );
startActivity(selectedIntent);
}
}
公共类BreakfastListView扩展了ListActivity{
静态最终字符串[]breakfood={
“草莓奶昔”,
“蛋松饼”,
“早晨圣代”,
“花生酱煎饼配香蕉”,
“泥灰岩士兵的蛋”,
“早餐玉米煎饼”,
“火腿奶酪松饼”,
“培根、鸡蛋和奶酪百吉饼”,
“酸奶糖浆”,
“热麦片粥”};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(新阵列适配器)(此,
android.R.layout.simple_list_item_1,breakfood));
//设置上面的列表数组
}
受保护的void onListItemClick(ListView lv、View v、int位置、长id){
super.onListItemClick(lv、v、位置、id);
字符串openbreakclass=breakfood[position];
如果(“草莓奶昔”。等于(openbreakclass))
{
Intent selectedIntent=新的Intent(v.getContext(),草莓酱Shake.class);
起始触感(选择内容);
}否则,如果(“蛋松饼”。等于(openbreakclass))
{
Intent selectedIntent=newintent(v.getContext(),Egg\u muffin.class);
起始触感(选择内容);
}否则,如果(“早晨圣代”。等于(openbreakclass)){
Intent selectedIntent=newintent(v.getContext(),Morning\u sundae.class);
起始触感(选择内容);
}否则如果(“花生酱煎饼w香蕉”。等于(openbreakclass)){
Intent selectedIntent=newintent(v.getContext(),花生酱煎饼w香蕉类);
起始触感(选择内容);
}否则,如果(“泥蛋和泥灰石士兵”。等于(openbreakclass)){
Intent selectedIntent=newintent(v.getContext(),Dippy_eggs_w_marmite_solids.class);
起始触感(选择内容);
}否则如果(“早餐玉米煎饼”。等于(openbreakclass)){
Intent selectedIntent=newintent(v.getContext(),早餐\u burrito.class);
起始触感(选择内容);
}否则如果(“火腿奶酪松饼”。等于(openbreakclass)){
Intent selectedIntent=newintent(v.getContext(),Ham_和_cheese_muffin.class);
起始触感(选择内容);
}否则,如果(“培根、鸡蛋和奶酪百吉饼”。等于(openbreakclass)){
Intent selectedIntent=newintent(v.getContext(),Bacon\u egg\u and\u cheese\u bagel.class);
起始触感(选择内容);
}else if(“酸奶混搭”.equals(openbreakclass)){
Intent selectedIntent=newintent(v.getContext(),yoghter\u mashup.class);
起始触感(选择内容);
}else if(“热麦片粥”。等于(openbreakclass)){
Intent selectedIntent=newintent(v.getContext(),Hot\u graines\u mashup.class);
起始触感(选择内容);
}
}
1st
我可以问你,如果列表视图中有100个项目,你会怎么做?检查数组中的每个项目
解决问题:
若要更改颜色、字体或其他属性,必须在适配器中执行
比如
...
View view=convertedView;
if(view ==null)
((TextView)view.findViewById(R.id.title)).setTextColor(Color.BLACK); // or what you want
...
已更新
因此,如果您想更改颜色,只需创建一个扩展类
BaseAdapter
(或类似)根据我的回答,更正getView
方法。你可以使用位置来引用数组中的索引,以删除大的if-else语句,用户在此处看到它会更容易。对不起,我真的没有那么抱歉,关于长代码,我算是个初学者。你会说在托盘中使用listview吗对我来说,这比直接引用listactivity类要容易得多listactivity只是ListView上的一个类似的包装器。