如何通过分隔行和列来制作网格视图?

时间:2023-02-09 22:31:25

how to generate an interface as the image shows using a gridview如何通过分隔行和列来制作网格视图?

如图所示使用gridview生成界面

As you can see, he has those jumps in the ranks.

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

3 个解决方案

#1


0  

try this..

尝试这个..

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

#2


0  

Add empty data into list

将空数据添加到列表中

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

In adapter getView()

在适配器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)
    }
}

#3


0  

Try my solution,

试试我的解决方案

Show stroke in between gridview layout rows and columns programmatically

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

Showing border between grid view items separate the GridView items so each and every item looks individually. In this tutorial we are creating a custom string array and setting up that string array into grid view using array adapter class. Now we are dynamically creating textview for each string element and set that element inside gird view one by one. So here is the complete step by step tutorial for How to display divider/border between GridView items in Android.

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

Code for MainActivity.java file

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>

Code for grid_items_border.xml file

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:

Screeshot:

如何通过分隔行和列来制作网格视图?

#1


0  

try this..

尝试这个..

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

#2


0  

Add empty data into list

将空数据添加到列表中

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

In adapter getView()

在适配器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)
    }
}

#3


0  

Try my solution,

试试我的解决方案

Show stroke in between gridview layout rows and columns programmatically

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

Showing border between grid view items separate the GridView items so each and every item looks individually. In this tutorial we are creating a custom string array and setting up that string array into grid view using array adapter class. Now we are dynamically creating textview for each string element and set that element inside gird view one by one. So here is the complete step by step tutorial for How to display divider/border between GridView items in Android.

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

Code for MainActivity.java file

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>

Code for grid_items_border.xml file

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:

Screeshot:

如何通过分隔行和列来制作网格视图?