android GridView,Gallery,ListView

原文:http://blog.csdn.net/wdaming1986/article/details/6628814

一、今天总结了下几种常用的控件GridView,Gallery,ListView,希望对大家有帮助,基本是改的例子程序,先看效果图:



    主页面效果:               点击GridView后出现的界面:

       

点击Gallery后出现的界面:            点击ListView后出现的界面:

     

 

二、先把xml文件的代码贴出来:

 1、main.xml文件中的代码

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  android:orientation="vertical" android:layout_width="fill_parent"  
  4.  android:layout_height="fill_parent">  
  5.  <TextView   
  6.      android:layout_width="fill_parent"  
  7.   android:layout_height="wrap_content"  
  8.      android:text="大明原创"  
  9.   android:gravity="center_horizontal"   
  10.   android:layout_marginBottom="5dip"  
  11.   android:layout_marginTop="5dip"/>  
  12.  <Button   
  13.      android:id="@+id/gridview_button"  
  14.   android:layout_width="fill_parent"   
  15.   android:layout_height="60dip"  
  16.   android:text="GridView"/>  
  17.  <Button   
  18.      android:id="@+id/gallery_button"  
  19.   android:layout_width="fill_parent"   
  20.   android:layout_height="60dip"  
  21.   android:text="Gallery"></Button>  
  22.  <Button   
  23.      android:id="@+id/listview_button"  
  24.   android:layout_width="fill_parent"   
  25.   android:layout_height="60dip"  
  26.   android:text="ListView"></Button>  
  27. </LinearLayout>  


2、gallery.xml中的代码:

[html]  view plain copy print ?
  1. <pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>  
  2. <Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery"  
  3.  android:layout_width="fill_parent"  
  4.  android:layout_height="wrap_content"  
  5. />  
  6.   
  7. </pre><br>  
  8. <pre></pre>  
  9. <p><span style="font-size:18px">3、gridview.xml中的代码:</span></p>  
  10. <span style="font-size:18px"></span><pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>  
  11. <GridView xmlns:android="http://schemas.android.com/apk/res/android"   
  12.     android:id="@+id/myGrid"  
  13.  android:layout_width="fill_parent"   
  14.  android:layout_height="fill_parent"  
  15.     android:padding="10dp"  
  16.     android:verticalSpacing="10dp"  
  17.       
  18.     android:horizontalSpacing="10dp"  
  19.     android:numColumns="auto_fit"  
  20.     android:columnWidth="60dp"  
  21.     android:stretchMode="columnWidth"  
  22.       
  23.     android:gravity="center"  
  24. />  
  25. </pre>  
  26. <p><br>  
  27. <span style="font-size:18px">4、listview.xml中的代码:</span></p>  
  28. <span style="font-size:18px"></span><pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>  
  29.   
  30. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  31.     android:orientation="vertical" android:layout_width="fill_parent"   
  32.     android:layout_height="fill_parent">   
  33.     <TextView android:text="用户列表" android:gravity="center"   
  34.         android:layout_height="wrap_content"   
  35.         android:layout_width="fill_parent" android:background="#DAA520"   
  36.         android:textColor="#000000">   
  37.     </TextView>   
  38.     <LinearLayout   
  39.         android:layout_width="wrap_content"   
  40.         android:layout_height="wrap_content">   
  41.         <TextView android:text="姓名"   
  42.             android:gravity="center" android:layout_width="150px"   
  43.             android:layout_height="wrap_content" android:textStyle="bold"   
  44.             android:background="#7CFC00">   
  45.         </TextView>   
  46.         <TextView android:text="年龄"   
  47.             android:layout_width="170px" android:gravity="center"   
  48.             android:layout_height="wrap_content" android:textStyle="bold"   
  49.             android:background="#F0E68C">   
  50.         </TextView>   
  51.     </LinearLayout>   
  52.     <ListView android:layout_width="wrap_content"   
  53.         android:layout_height="wrap_content" android:id="@+id/users">   
  54.     </ListView>   
  55. </LinearLayout>   
  56.   
  57.   
  58. </pre>  
  59. <p><br>  
  60. <span style="font-size:24px">5、user.xml中的代码:</span></p>  
  61. <span style="font-size:18px"></span><pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>  
  62. <TableLayout      
  63.          android:layout_width="fill_parent"      
  64.          xmlns:android="http://schemas.android.com/apk/res/android"      
  65.          android:layout_height="wrap_content"      
  66.          >   
  67.          <TableRow >   
  68.          <ImageView      
  69.                android:layout_width="wrap_content"      
  70.                android:layout_height="wrap_content"  
  71.                android:layout_marginRight="10dip"     
  72.                android:id="@+id/img">      
  73.          </ImageView>    
  74.          <TextView      
  75.                android:layout_height="wrap_content"      
  76.                android:layout_width="150px"   
  77.                android:gravity="center_vertical"     
  78.                android:layout_gravity="center_vertical"  
  79.                android:id="@+id/name">    
  80.          </TextView>    
  81.          <TextView      
  82.                android:layout_height="wrap_content"     
  83.                android:layout_width="170px"   
  84.                android:gravity="center_vertical"    
  85.                android:layout_gravity="center_vertical"   
  86.                android:id="@+id/age">    
  87.          </TextView>   
  88.          </TableRow>   
  89. </TableLayout>  
  90.   
  91. </pre>  
  92. <p><br>  
  93. <span style="font-size:18px">三,java类中的代码:</span></p>  
  94. <p><span style="font-size:18px">1、MainActivity类中的代码:</span></p>  
  95. <span style="font-size:18px"></span><pre class="java" name="code">package com.cn.android;  
  96.   
  97. import android.app.Activity;  
  98. import android.content.Intent;  
  99. import android.os.Bundle;  
  100. import android.view.View;  
  101. import android.view.View.OnClickListener;  
  102. import android.widget.Button;  
  103.   
  104. public class MainActivity extends Activity {  
  105.     /** Called when the activity is first created. */  
  106.    
  107.     @Override  
  108.     public void onCreate(Bundle savedInstanceState) {  
  109.         super.onCreate(savedInstanceState);  
  110.         setContentView(R.layout.main);  
  111.         //初始化button按钮  
  112.         initGridViewGalleryListViewButton();  
  113.     }  
  114.   
  115.     //实现button按钮的方法  
  116.  private void initGridViewGalleryListViewButton() {  
  117.   // TODO Auto-generated method stub  
  118.   //跳转到GridView的Actvity中  
  119.   Button GridViewButton = (Button)findViewById(R.id.gridview_button);  
  120.   GridViewButton.setOnClickListener(new OnClickListener(){  
  121.   
  122.    public void onClick(View arg0) {  
  123.     // TODO Auto-generated method stub  
  124.     Intent i_gridview = new Intent();  
  125.     i_gridview.setClass(MainActivity.this, GridViewActivity.class);  
  126.     startActivity(i_gridview);  
  127.       
  128.    }  
  129.   });  
  130.     
  131.   //跳转到Gallery的Activity中  
  132.   Button GalleryButton = (Button)findViewById(R.id.gallery_button);  
  133.   GalleryButton.setOnClickListener(new OnClickListener(){  
  134.   
  135.    public void onClick(View arg0) {  
  136.     // TODO Auto-generated method stub  
  137.     Intent i_gallery = new Intent();  
  138.     i_gallery.setClass(MainActivity.this, GalleryActivity.class);  
  139.     startActivity(i_gallery);  
  140.    }  
  141.   });  
  142.     
  143.   //跳转到ListView的Activity中  
  144.   Button ListViewButton = (Button)findViewById(R.id.listview_button);  
  145.   ListViewButton.setOnClickListener(new OnClickListener(){  
  146.   
  147.    public void onClick(View arg0) {  
  148.     // TODO Auto-generated method stub  
  149.     Intent i_listview = new Intent();  
  150.     i_listview.setClass(MainActivity.this,ListViewActivity.class);  
  151.     startActivity(i_listview);  
  152.    }  
  153.   });  
  154.  }  
  155. }  
  156.   
  157. </pre>  
  158. <p><br>  
  159. <span style="font-size:24px">2、GridViewActivity中的代码:</span></p>  
  160. <span style="font-size:18px"></span><pre class="java" name="code">package com.cn.android;  
  161.   
  162. import android.app.Activity;  
  163. import android.content.Context;  
  164. import android.os.Bundle;  
  165. import android.view.View;  
  166. import android.view.ViewGroup;  
  167. import android.widget.BaseAdapter;  
  168. import android.widget.GridView;  
  169. import android.widget.ImageView;  
  170.   
  171. public class GridViewActivity extends Activity{  
  172.   
  173.  @Override  
  174.  protected void onCreate(Bundle savedInstanceState) {  
  175.   // TODO Auto-generated method stub  
  176.   super.onCreate(savedInstanceState);  
  177.   setContentView(R.layout.gridview);  
  178.   GridView gridview = (GridView) findViewById(R.id.myGrid);  
  179.         gridview.setAdapter(new ImageAdapter(this));  
  180.  }  
  181.  //定义ImageAdapter类  
  182.  public class ImageAdapter extends BaseAdapter {  
  183.     
  184.         private Context mContext;  
  185.     
  186.         public ImageAdapter(Context c) {  
  187.             mContext = c;  
  188.         }  
  189.   
  190.         public int getCount() {  
  191.             return mThumbIds.length;  
  192.         }  
  193.   
  194.         public Object getItem(int position) {  
  195.             return position;  
  196.         }  
  197.   
  198.         public long getItemId(int position) {  
  199.             return position;  
  200.         }  
  201.   
  202.         public View getView(int position, View convertView, ViewGroup parent) {  
  203.             ImageView imageView;  
  204.             if (convertView == null) {  
  205.                 imageView = new ImageView(mContext);  
  206.                 imageView.setLayoutParams(new GridView.LayoutParams(50, 50));  
  207.                 imageView.setAdjustViewBounds(false);  
  208.                 imageView.setScaleType(ImageView.ScaleType.FIT_XY);  
  209.                 imageView.setPadding(10, 10, 10, 10);  
  210.             } else {  
  211.                 imageView = (ImageView) convertView;  
  212.             }  
  213.   
  214.             imageView.setImageResource(mThumbIds[position]);  
  215.   
  216.             return imageView;  
  217.         }  
  218.   
  219.         private Integer[] mThumbIds = {  
  220.                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,  
  221.                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,  
  222.                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,  
  223.                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,  
  224.                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,  
  225.                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,  
  226.                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,  
  227.                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,  
  228.                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,  
  229.                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,  
  230.                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,  
  231.                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,  
  232.         };  
  233.     }  
  234. }  
  235.   
  236. </pre>  
  237. <p><br>  
  238. <span style="font-size:24px">3、GalleryActivity中的代码:</span></p>  
  239. <span style="font-size:18px"></span><pre class="java" name="code">package com.cn.android;  
  240.   
  241. import android.app.Activity;  
  242. import android.content.Context;  
  243. import android.content.res.TypedArray;  
  244. import android.os.Bundle;  
  245. import android.view.ContextMenu;  
  246. import android.view.MenuItem;  
  247. import android.view.View;  
  248. import android.view.ViewGroup;  
  249. import android.view.ContextMenu.ContextMenuInfo;  
  250. import android.widget.AdapterView;  
  251. import android.widget.BaseAdapter;  
  252. import android.widget.Gallery;  
  253. import android.widget.ImageView;  
  254. import android.widget.Toast;  
  255. import android.widget.AdapterView.AdapterContextMenuInfo;  
  256. import android.widget.AdapterView.OnItemClickListener;  
  257.   
  258. public class GalleryActivity extends Activity{  
  259.   
  260.  @Override  
  261.  protected void onCreate(Bundle savedInstanceState) {  
  262.   // TODO Auto-generated method stub  
  263.   super.onCreate(savedInstanceState);  
  264.   setContentView(R.layout.gallery);  
  265.    // Reference the Gallery view  
  266.         Gallery gallery = (Gallery) findViewById(R.id.gallery);  
  267.         // Set the adapter to our custom adapter (below)  
  268.         gallery.setAdapter(new ImageAdapter(this));  
  269.           
  270.         // Set a item click listener, and just Toast the clicked position  
  271.         gallery.setOnItemClickListener(new OnItemClickListener() {  
  272.             public void onItemClick(AdapterView parent, View v, int position, long id) {  
  273.                 Toast.makeText(GalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();  
  274.             }  
  275.         });  
  276.           
  277.         // We also want to show context menu for longpressed items in the gallery  
  278.         registerForContextMenu(gallery);  
  279.  }  
  280.   
  281.   @Override  
  282.      public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {  
  283.          menu.add(R.string.gallery_text);  
  284.      }  
  285.        
  286.      @Override  
  287.      public boolean onContextItemSelected(MenuItem item) {  
  288.          AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();  
  289.          Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();  
  290.          return true;  
  291.      }  
  292.   
  293.      public class ImageAdapter extends BaseAdapter {  
  294.          int mGalleryItemBackground;  
  295.          private Context mContext;  
  296.            
  297.          public ImageAdapter(Context c) {  
  298.              mContext = c;  
  299.              // See res/values/attrs.xml for the <declare-styleable> that defines  
  300.              // Gallery1.  
  301.              TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);  
  302.              mGalleryItemBackground = a.getResourceId(  
  303.                      R.styleable.Gallery1_android_galleryItemBackground, 0);  
  304.              a.recycle();  
  305.          }  
  306.   
  307.          public int getCount() {  
  308.              return mImageIds.length;  
  309.          }  
  310.   
  311.          public Object getItem(int position) {  
  312.              return position;  
  313.          }  
  314.   
  315.          public long getItemId(int position) {  
  316.              return position;  
  317.          }  
  318.   
  319.          public View getView(int position, View convertView, ViewGroup parent) {  
  320.              ImageView i = new ImageView(mContext);  
  321.   
  322.              i.setImageResource(mImageIds[position]);  
  323.              i.setScaleType(ImageView.ScaleType.FIT_XY);  
  324.              i.setLayoutParams(new Gallery.LayoutParams(136, 88));  
  325.                
  326.              // The preferred Gallery item background  
  327.              i.setBackgroundResource(mGalleryItemBackground);  
  328.                
  329.              return i;  
  330.          }    
  331.   
  332.          private Integer[] mImageIds = {  
  333.                  R.drawable.gallery_photo_1,  
  334.                  R.drawable.gallery_photo_2,  
  335.                  R.drawable.gallery_photo_3,  
  336.                  R.drawable.gallery_photo_4,  
  337.                  R.drawable.gallery_photo_5,  
  338.                  R.drawable.gallery_photo_6,  
  339.                  R.drawable.gallery_photo_7,  
  340.                  R.drawable.gallery_photo_8  
  341.          };  
  342.      }  
  343. }  
  344.   
  345. </pre>  
  346. <p><br>  
  347. <span style="font-size:24px">4、ListViewActivity中的代码</span></p>  
  348. <span style="font-size:18px"></span><pre class="java" name="code">package com.cn.android;  
  349.   
  350. import java.util.ArrayList;  
  351. import java.util.HashMap;  
  352.   
  353. import android.app.Activity;  
  354. import android.os.Bundle;  
  355. import android.widget.ListView;  
  356. import android.widget.SimpleAdapter;  
  357.   
  358. public class ListViewActivity extends Activity{  
  359.   
  360.  @Override  
  361.  protected void onCreate(Bundle savedInstanceState) {  
  362.   // TODO Auto-generated method stub  
  363.   super.onCreate(savedInstanceState);  
  364.    setContentView(R.layout.listview);   
  365.          ArrayList<HashMap<String, Object>> users = new ArrayList<HashMap<String, Object>>();   
  366.          for (int i = 0; i < 10; i++) {   
  367.              HashMap<String, Object> user = new HashMap<String, Object>();   
  368.              user.put("img", R.drawable.icon);   
  369.              user.put("username", "姓名(" + i+")");   
  370.              user.put("age", (20 + i) + "");   
  371.              users.add(user);   
  372.          }   
  373.          SimpleAdapter saImageItems = new SimpleAdapter(this,   
  374.                  users,// 数据来源   
  375.                  R.layout.user,//每一个user xml 相当ListView的一个组件   
  376.                  new String[] { "img", "username", "age" },   
  377.                  // 分别对应view 的id   
  378.                  new int[] { R.id.img, R.id.name, R.id.age });   
  379.          // 获取listview   
  380.          ((ListView) findViewById(R.id.users)).setAdapter(saImageItems);   
  381.  }  
  382.   
  383. }  
  384.   
  385.   
  386. </pre>  
  387. <p><br>  
  388. <span style="font-size:24px"><strong>四、在values的文件夹下面建一个attrs.xml文件:gallery设置样式的功能</strong></span></p>  
  389. <p><span style="font-size:18px"><?xml version="1.0" encoding="utf-8"?><br>  
  390. <resources>    <br>  
  391. </span></p>  
  392. <pre class="html" name="code">    <!-- These are the attributes that we want to retrieve from the theme  
  393.          in view/Gallery1.java -->  
  394.     <declare-styleable name="Gallery1">  
  395.         <attr name="android:galleryItemBackground" />  
  396.     </declare-styleable>      
  397. </resources>  
  398. </pre>  
  399. <p><br>  
  400.  </p>  
  401. <p><span style="font-size:24px"><strong>五、Manifest.xml文件中的代码:</strong></span></p>  
  402. <span style="font-size:18px"></span><pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>  
  403. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  404.       package="com.cn.android"  
  405.       android:versionCode="1"  
  406.       android:versionName="1.0">  
  407.     <uses-sdk android:minSdkVersion="7" />  
  408.   
  409.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  410.         <activity android:name=".MainActivity"  
  411.                   android:label="@string/app_name">  
  412.             <intent-filter>  
  413.                 <action android:name="android.intent.action.MAIN" />  
  414.                 <category android:name="android.intent.category.LAUNCHER" />  
  415.             </intent-filter>  
  416.         </activity>  
  417.         <activity android:name=".GridViewActivity" android:label="GridViewActivity"></activity>  
  418.         <activity android:name=".GalleryActivity"  android:label="GalleryActivity"></activity>  
  419.         <activity android:name=".ListViewActivity" android:label="ListViewActivity"></activity>  
  420.     </application>  
  421. </manifest>  
  422.   
  423. </pre>  
  424. <p><br>  
  425. <span style="font-size:24px"><strong>六、特别说明:程序中有的图片我没有添加,大家找一些图片特换掉就行了,大小不合适的就调一下布局,设置一些参数就可以了!希望大家谅解:</strong></span></p>  
  426. <p><strong><span style="font-size:24px">drawable目录下的图片:</span></strong></p>  
  427. <p><strong><span style="font-size:24px"><span style="font-size:24px"><strong><img alt="" src="http://hi.csdn.net/attachment/201107/23/0_1311438621TDdN.gif"></strong></span></span></strong></p>  
  428. <p><strong><span style="font-size:24px"></span></strong> </p>  
  429. <p><strong><span style="font-size:24px"></span></strong> </p>  
  430. <p><strong><span style="font-size:24px"></span></strong> </p>  
  431. <p><strong><span style="font-size:24px"></span></strong> </p>  
  432. <p><strong><span style="font-size:24px"></span></strong> </p>  
  433. <p> </p>  
  434. <pre></pre>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值