如何设置备用行颜色在GridView的Android? [英] How to set Alternate Row Color in a Gridview Android?
本文介绍了如何设置备用行颜色在GridView的Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何设置在GridView交替行的颜色?我寻觅了有关如何设置网格视图行的颜色,但没有约的GridView行的颜色很多教程。我只用备用行颜色列表视图。我需要备用行应该是白色和黑色。在这里,我包括我的codeS。请帮我!!!!!!!!!!
How to set alternate row color in a gridview? I have searched a lot of tutorial about how to set grid view row colors,but nothing about gridview row color. I got list view with alternate row color only. I need alternate row should be white and black. Here I Include my codes. Please help me!!!!!!!!!!
这就是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>
请提供良好的教程或样本为例。由于提前!!!!!!!!!!!
Please provide good tutorial or sample Example. Thanks in Advance!!!!!!!!!!!
推荐答案
创建自定义适配器,并覆盖其 getView
方法:
Create a custom adapter and override its getView
method:
@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>
这篇关于如何设置备用行颜色在GridView的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文