本範例以BackgroundAdapter.java extends BaseAdapter來改變GridView的背景圖, 並以一個覆蓋全畫面的ImageView增添OnClickListener事件, 讓user可以切換色系的名稱與編碼.
另外為了讓畫面保持在橫向狀態, 必須在Manifest.xml的Activity標籤中加入android:screenOrientation="landscape"的屬性
GridViewActivity.java
package com.tsots.GridView_Adapter_Color; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.GridView; import android.widget.ImageView; public class GridViewActivity extends Activity { GridView gridview; ImageView imageview; String[] items; int temp = 0; BackgroundAdapter mygridviewadapter; int[] color = { R.color.darkred, R.color.crimson, R.color.red, R.color.firebrick, R.color.purple, R.color.blueviolet, R.color.orchid, R.color.pink, R.color.mediumvioletred, R.color.deeppink, R.color.magenta, R.color.lightcoral, R.color.darkgreen, R.color.green, R.color.limegreen, R.color.lightgreen, R.color.lime, R.color.lawngreen, R.color.greenyellow, R.color.paleolivine, R.color.darkblue, R.color.blue, R.color.paleblue, R.color.lightblue, R.color.darkaqua, R.color.aqua, R.color.paleaqua, R.color.lightaqua, R.color.black, R.color.gray, R.color.silver, R.color.white, R.color.darkorange, R.color.orange, R.color.paleorange, R.color.lightorange, R.color.gold, R.color.yellow, R.color.paleyellow, R.color.lightyellow }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gridview = (GridView) findViewById (R.id.gridview); imageview = (ImageView) findViewById (R.id.imageview); items = getResources().getStringArray(R.array.colorname_array); imageview.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (temp == 0) { items = getResources().getStringArray(R.array.colorcode_array); temp = 1; } else { items = getResources().getStringArray(R.array.colorname_array); temp = 0; } mygridviewadapter = new BackgroundAdapter(GridViewActivity.this, items, color); gridview.setAdapter(mygridviewadapter); } }); mygridviewadapter = new BackgroundAdapter(GridViewActivity.this, items, color); gridview.setAdapter(mygridviewadapter); } }
在這個類別中, 用LayoutInflater讀入自定的layout(只放一個TextView),便可以陣列各別setText與setBackgroundResource
BackgroundAdapter.java
package com.tsots.GridView_Adapter_Color; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class BackgroundAdapter extends BaseAdapter { private Context _con; private String[] _items; private int[] _icons; public BackgroundAdapter(Context con,String[] items,int[] icons) { _con=con; _items=items; _icons=icons; } public int getCount() { return _items.length; } public Object getItem(int arg0) { return _items[arg0]; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater factory = LayoutInflater.from(_con); View v = (View) factory.inflate(R.layout.grid, null); TextView tv = (TextView) v.findViewById(R.id.text); tv.setBackgroundResource(_icons[position]); tv.setText(_items[position]); return v; } }
您好~ 想請問 colorname_array 這個部分該怎麼使用~ 編譯這部分出錯~
回覆刪除Hi~
回覆刪除請問錯誤訊息?
我猜測你可能沒有建立colors.xml (內含各種顏色的定義)
以及strings.xml中會定義colorname_array(定義字串陣列)喔!
恩 ~查看之下是這個問題~~
回覆刪除在請問一下~ 表單載入時著色 是tv.setBackgroundResource(_icons[position]);作用的嗎~
如果我要設定成我要的顏色~ 是對 colorname_array(定義字串陣列) 這邊下手嗎~
如果要設定你要的顏色, 是建立在新增的colors.xml中喔.
回覆刪除而 colorname_array是定義文字(說明這是什麼色、那是什麼色)
作者已經移除這則留言。
回覆刪除