如何通过分隔行和列来创建gridview?

时间:2017-12-21 05:05:00

标签: android gridview

如图所示,使用gridview enter image description here

生成界面

正如你所看到的,他在队伍中有这些跳跃。

3 个答案:

答案 0 :(得分:0)

试试这个..

 <?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>

答案 1 :(得分:0)

将空数据添加到列表中

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)
    }
}

答案 2 :(得分:0)

试试我的解决方案,

以编程方式在gridview布局行和列之间显示笔划

  

在网格视图项之间显示边框,将GridView分开   项目,以便每个项目单独查看。在本教程中我们   正在创建自定义字符串数组并设置该字符串数组   使用数组适配器类进入网格视图。现在我们是动态的   为每个字符串元素创建textview并设置该元素   里面的网格视图一个接一个。所以这是完整的一步一步   如何显示GridView项之间的分隔符/边框的教程   在Android中。

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>

<强> Screeshot:

enter image description here