zoukankan      html  css  js  c++  java
  • gridview--基本的gridview

    GridView 元素距离设定

    因为该设定比较简单 防止以后忘记 所以贴 供自己查阅

    1. 布局:main.xml

    Java代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7. <GridView  
    8.     android:id="@+id/grid"    
    9.     android:layout_width="fill_parent"   
    10.     android:layout_height="wrap_content"   
    11.     android:layout_centerInParent="true"  
    12.       
    13.     android:horizontalSpacing="50dp"   
    14.     android:verticalSpacing="50dp"   
    15.     />  
    16. </RelativeLayout>  

    2. *.jave: GriUsage.java

    Java代码  收藏代码
    1. public class GridUsage extends Activity {  
    2.     GridView grid;  
    3.     ImageAdapter iAdapter;  
    4.       
    5.     String[] text = {  
    6.             "one","two","three","four","five","six","seven","eight","nine","ten"  
    7.     };  
    8.       
    9.     /** Called when the activity is first created. */  
    10.     @Override  
    11.     public void onCreate(Bundle savedInstanceState) {  
    12.         super.onCreate(savedInstanceState);  
    13.         setContentView(R.layout.main);  
    14.           
    15.         grid = (GridView)findViewById(R.id.grid);  
    16.         iAdapter = new ImageAdapter(this);  
    17.           
    18.         grid.setAdapter(iAdapter);  
    19.         grid.setNumColumns(3);  
    20.           
    21.     }  
    22.       
    23.     public class ImageAdapter extends BaseAdapter {  
    24.         Activity activity;  
    25.           
    26.         public ImageAdapter(Activity a){  
    27.             activity = a;  
    28.         }  
    29.         @Override  
    30.         public int getCount() {  
    31.             // TODO Auto-generated method stub  
    32.             return text.length;  
    33.         }  
    34.   
    35.         @Override  
    36.         public Object getItem(int arg0) {  
    37.             // TODO Auto-generated method stub  
    38.             return null;  
    39.         }  
    40.   
    41.         @Override  
    42.         public long getItemId(int arg0) {  
    43.             // TODO Auto-generated method stub  
    44.             return arg0;  
    45.         }  
    46.   
    47.         @Override  
    48.         public View getView(int position, View convertView, ViewGroup parent) {  
    49.             // TODO Auto-generated method stub  
    50.             TextView tv;  
    51.               
    52.             if(convertView == null){  
    53.                 tv = new TextView(activity);  
    54.             }  
    55.             else {  
    56.                 tv = (TextView)convertView;  
    57.             }  
    58.               
    59.             tv.setSingleLine(true);  
    60.             tv.setBackgroundResource(R.drawable.back);  
    61.             tv.setGravity(Gravity.CENTER);  
    62.             tv.setText(text[position]);  
    63.             return tv;  
    64.         }  
    65.           
    66.     }  
    67.       
    68. }  

    3. emulator 运行截图:

    that's all.  any idea or other are welcome!!!!

  • 相关阅读:
    (二分查找 拓展) leetcode 69. Sqrt(x)
    (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element
    (链表) lintcode 219. Insert Node in Sorted Linked List
    (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range
    (最短路 Floyd) P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 洛谷
    (字符串 数组 递归 双指针) leetcode 344. Reverse String
    (二叉树 DFS 递归) leetcode 112. Path Sum
    (二叉树 DFS 递归) leetcode 101. Symmetric Tree
    (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
    (二叉树 递归 DFS) leetcode 100. Same Tree
  • 原文地址:https://www.cnblogs.com/awkflf11/p/5061904.html
Copyright © 2011-2022 走看看