如何通过分隔行和列来创建gridview?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过分隔行和列来创建gridview?相关的知识,希望对你有一定的参考价值。
答案
试试这个..
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:scrollbarStyle="outsideOverlay"
android:verticalScrollbarPosition="right"
android:scrollbars="vertical">
</GridView>
另一答案
将空数据添加到列表中
ArrayList<Data> list=new ArrayList<Data>
list.add(new Data(G13));
list.add(new Data(G23));
list.add(new Data(0);//set id=0
list.add(new Data(G43));
在适配器getView()中
public View getView(int position, View convertView, ViewGroup parent) {
if(list.get(position).getId().equals("0")){//empty data
view.setVisibility(View.INVISIBLE)
}else{
view.setVisibility(View.VISIBLE)
}
}
另一答案
试试我的解决方案
以编程方式在gridview布局行和列之间显示笔划
显示网格视图项之间的边界将GridView项目分开,以便每个项目单独查看。在本教程中,我们将创建一个自定义字符串数组,并使用数组适配器类将该字符串数组设置为网格视图。现在我们为每个字符串元素动态创建textview,并在gird视图中逐个设置该元素。所以这里是如何在Android中显示GridView项目之间的分隔符/边框的完整分步教程。
MainActivity.java文件的代码
public class MainActivity extends Activity {
GridView gridView;
List<String> ItemsList;
String[] itemsName = new String[]{
"ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT","NINE"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView)findViewById(R.id.gridView1);
ItemsList = new ArrayList<String>(Arrays.asList(itemsName));
gridView.setAdapter(new TextAdapter(this));
}
private class TextAdapter extends BaseAdapter
{
Context context;
public TextAdapter(Context context)
{
this.context = context;
}
@Override
public int getCount() {
return itemsName.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemsName[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView text = new TextView(this.context);
text.setText(itemsName[position]);
text.setGravity(Gravity.CENTER);
text.setBackgroundColor(Color.parseColor("#fbdcbb"));
text.setLayoutParams(new GridView.LayoutParams(144, 144));
text.setBackgroundResource(R.layout.grid_items_border);
return text;
}
}
}
**Code for activity_main.xml layout file**
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.gridviewwithborder_android_examples.com.MainActivity">
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:numColumns="3" >
</GridView>
</RelativeLayout>
grid_items_border.xml文件的代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#fafbfb" />
<stroke
android:width="2dp"
android:color="#fd0261" ></stroke>
</shape>
截图:
以上是关于如何通过分隔行和列来创建gridview?的主要内容,如果未能解决你的问题,请参考以下文章
确定是不是可以通过翻转 1 矩阵的行和列来达到给定二进制矩阵的算法