如何更改所选项目的颜色&设置字体&列表视图中的颜色

时间:2013-09-21 19:14:28

标签: android listview

我使用意图创建了一个列表视图但是为了改善我的应用程序的用户体验我如何更改列表视图中所选项目的颜色我直接使用了ListActivity类而不是从调色板中的列表视图所以我需要将代码直接添加到活动这里是我的代码

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);
        }

    }

1 个答案:

答案 0 :(得分:0)

<强>第一

我可以问你,如果列表视图中有100项,你会怎么做?你检查阵列中的每一项?

问题:

如果要更改颜色或字体或其他属性,则必须在适配器中执行此操作。

...
View view=convertedView;
if(view ==null)
    ((TextView)view.findViewById(R.id.title)).setTextColor(Color.BLACK); // or what you want
...

<强>更新

因此,如果你想改变颜色,只需创建一个扩展BaseAdapter(或类似)的类,并根据我的回答纠正getView方法。