[問題] gridView 不同背景色

看板AndroidDev作者 (麵T)時間12年前 (2014/03/11 13:45), 編輯推噓3(302)
留言5則, 3人參與, 最新討論串1/1
想在螢幕上做n*n的網格,每個網格會有不同的顏色(黑色或白色) 並且要能隨時間由黑變白或由白變黑。 我的做法是: 在 drawable 放入黑、白兩張圖, 在 GridView 上產生n*n個 ImageView 再讀入所對應的圖。執行果相當耗時間, 不知道能不能直接在n*n的 GridView 中直接著色?不要使用 ImageView -- 我是麵T,哩賀 http://ppt.cc/-eS5 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.172.56.21

03/12 15:49, , 1F
adapter的getItem中設定背景顏色
03/12 15:49, 1F

03/13 00:19, , 2F
adapter的getView()裡面 convertView.setBackgroundColor
03/13 00:19, 2F

03/13 00:20, , 3F
(Color.BLACK or Color.WHITE);
03/13 00:20, 3F
昨天我嘗試: TextView[] pixel = new TextView[900]; GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout); for(int i = 0;i<900;i++){ pixel[i] = new TextView(this); gridLayout.addView(pixel[i]); pixel[i].setBackgroundColor(Color.YELLOW); } 但這樣子載入速度也是很慢。 會造成速度慢的原因是上面的程式碼產生過多的 View 嗎? 使用 getItem 的方法可以避免產生一堆 View? 謝謝 ※ 編輯: noodleT 來自: 61.227.97.248 (03/13 11:29)

03/13 14:06, , 4F
用Adapter 官方有教學 http://ppt.cc/8mp7
03/13 14:06, 4F

03/13 14:07, , 5F
在Adapter#getView()裡 去設View的background
03/13 14:07, 5F
文章代碼(AID): #1J7gBS5A (AndroidDev)