如何在网格中设置备用单元格颜色? 我发现了很多问题/教程 关于如何设置行颜色但没有 关于细胞的颜色。 提前致谢
答案 0 :(得分:3)
如果你有很多listView的例子,为什么不使用getView方法,因为getView方法用于适配器,而适配器用于视图,列表和网格。只需根据适配器视图中的视图位置设置视图背景。
protected void getView(AdapterView<> adapterView, View convertView, int position, long id)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View view =inflater.inflate(yourlayout.xml, null);
if(position%2==0)
view.setBackgroundColor(color1);
else
view.setBackgroundColor(color2);
return view;
}
答案 1 :(得分:3)
向前迈出一步,它有效
public class ListViewA extends Activity {
GridView MyGrid;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyGrid = (GridView) findViewById(R.id.gridview);
MyGrid.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
Context MyContext;
public ImageAdapter(Context _MyContext) {
MyContext = _MyContext;
}
@Override
public int getCount() {
return 9;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
view = li.inflate(R.layout.main, null);
}
if (position % 2 == 0)
view.setBackgroundColor(0x30FF0000);
else
view.setBackgroundColor(0x300000FF);
return view;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}
}