How to set the background colour of individual gridview cells

后端 未结 3 820
别跟我提以往
别跟我提以往 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:50

    Main activity where you have to create array of hexadecimal color code and and pass to custom adapter class

    public class MainActivity extends Activity {
    GridView gridView;
    
    
        String[] gridColor ={
    
                "#008B8B",
                "#00FF00",
                "#48D1CC",
                "#556B2F",
                "#696969",
                "#6B8E23",
                "#8FBC8F",
                "#AFEEEE",
                "#B8860B",
                "#BDB76B",
                "#D8BFD8",
                "#DEB887",
                "#FFFF00",
                "#FFF0F5",
                "#EE82EE",
                "#DC143C",
                "#C0C0C0"
        };
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Grid adapter = new Grid(MainActivity.this,gridColor);
            gridView=(GridView)findViewById(R.id.grid_view);
    
            gridView.setAdapter(adapter);
    
    
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView parent, View view,
                                        int position, long id)
                {
                    Toast.makeText(MainActivity.this, "You Clicked On " +gridcolor[+ position], Toast.LENGTH_SHORT).show();
    
                }
            });
    
        }
    
    }
    

    In custom adapter code will be like this where color code will be parsed

    public class Grid extends BaseAdapter {
    
        private Context mContext;
        private final String[] menu;
        private final int[] Imageid;
        private final String[] gridcolor;
    
        public Grid(Context context,String[] menu,int[] Imageid,String[] gridcolor)
        {
            mContext=context;
    
            this.gridcolor=gridcolor;
        }
    
        @Override
        public int getCount() {
            return gridcolor.length;
        }
    
        @Override
        public Object getItem(int i) {
            return null;
        }
    
        @Override
        public long getItemId(int i) {
            return 0;
        }
    
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View grid;
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            if (view == null) {
    
                grid = new View(mContext);
                grid = inflater.inflate(R.layout.grid_layout, null);
    
                grid.setBackgroundColor(Color.parseColor(gridcolor[i]));
    
            } else
            {
                grid =  view;
            }
    
            return grid;
        }
    }
    

提交回复
热议问题