How to set the background colour of individual gridview cells

后端 未结 3 807
别跟我提以往
别跟我提以往 2021-02-04 13:19

I have a GridView with each cell containing some text, and I want to be able to set the background colour of individual cells.

The XML for my GridView is:



        
3条回答
  •  别那么骄傲
    2021-02-04 13:46

    You need to create a custom layout and use it in your adapter instead of android.R.layout.simple_list_item_1. For example:

    
    

    (I simply copied the latest version of simple_list_item_1.xml and added the new background color at the end.)

    Save this as grid_layout.xml in res/layout and change your adapter's constructor to:

    ArrayAdapter adapter = new ArrayAdapter(this, R.layout.grid_layout, student_array);
    

提交回复
热议问题