如何在gridview中设置备用行颜色?我已经搜索了很多关于如何设置网格视图行颜色的教程,但没有关于gridview行颜色的内容。我只获得了备用行颜色的列表视图。我需要替代行应该是白色和黑色。在这里,我包括我的代码。请帮帮我!!!!!!!!!!
这是Java类:
public class MainActivity extends Activity {
GridView gridView;
static final String[] numbers = new String[] { "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, numbers);
gridView.setAdapter(adapter);
}
}
这是xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:numColumns="10"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</GridView>
请提供良好的教程或示例示例。在此先感谢!!!!!!!!!!!
答案 0 :(得分:5)
创建自定义适配器并覆盖其getView
方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridview1);
MyAdapter adapter = new MyAdapter(this,
R.layout.item, numbers);
gridView.setAdapter(adapter);
}
public class MyAdapter extends ArrayAdapter<String> {
String[] objects;
Context context;
public MyAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.objects = objects;
}
@Override
public View getView(int position, android.view.View convertView, android.view.ViewGroup parent) {
TextView tv;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
tv = (TextView)inflater.inflate(R.layout.item,parent,false);
} else {
tv = (TextView) convertView;
}
tv.setText(objects[position]);
if (position % 2 == 0)
tv.setBackgroundColor(Color.BLACK);
else
tv.setBackgroundColor(Color.WHITE);
return tv;
}
}
<强> item.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TextView>
答案 1 :(得分:0)
使用自定义适配器和自定义适配器
你将有方法 getView
在getView方法
中使用此代码if(position % ( 2*columnCount ) == 0)
{
// set first color
}
else
{
// set alternate row's color
}