zoukankan      html  css  js  c++  java
  • 好记性不如烂笔杆android学习笔记<十五> GridView简单用法

    需要达到的效果是:点击菜单中选项,添加表情图标,图标放在一个dialog类型的activity中:

    首先在manifest中注册这个activity:

    1        <activity android:name=".IconDialog"
    2                 android:configChanges="orientation"
    3                 android:theme="@android:style/Theme.Dialog"
    4             />

    使用GridView布局,显示图标

    icon_dialog_activity.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout 
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     android:id="@+id/launch_linear"
     5     android:layout_width="fill_parent" 
     6     android:layout_height="fill_parent"
     7     android:background="@color/half_transparent"
     8     android:orientation="vertical"
     9     >
    10        <GridView
    11             android:id="@+id/gridview"
    12             android:layout_width="fill_parent" 
    13             android:layout_height="fill_parent"
    14             android:numColumns="auto_fit"
    15             android:verticalSpacing="5dip"
    16             android:horizontalSpacing="5dip"
    17             android:columnWidth="60dip"
    18             android:stretchMode="columnWidth"
    19             android:gravity="center"
    20         />
    21 </LinearLayout>

    Java文件中,如下使用

      1 public class IconDialog extends Activity  {
      2     @Override
      3     protected void onCreate(Bundle savedInstanceState) {
      4         super.onCreate(savedInstanceState);
      5         setContentView(R.layout.icon_dialog_activity);
      6         //取得GridView对象
      7         GridView gridview = (GridView) findViewById(R.id.gridview);        
      8         //添加元素给gridview
      9         gridview.setAdapter(new ImageAdapter(this));
     10         // 设置Gallery的背景
     11         gridview.setBackgroundResource(R.drawable.paper_01);
     12         //事件监听
     13         gridview.setOnItemClickListener(new OnItemClickListener() {
     14             public void onItemClick(AdapterView<?> parent, View v, int position, long id)
     15             {
     16                 Toast.makeText(IconDialog.this, " the " + (position + 1) , Toast.LENGTH_SHORT).show();
     17                 
     18                 Intent resultValue = new Intent(IconDialog.this,MiniNoteConfige.class);
     19                 resultValue.putExtra("icon",position);
     20                 setResult(RESULT_OK, resultValue);
     21                 finish();
     22             } 
     23         });
     24     }
     25     
     26     public class ImageAdapter extends BaseAdapter{
     27         // 定义Context
     28         private Context        mContext;
     29         // 定义整型数组 即图片源
     30         private Integer[]    mImageIds    = {
     31                 R.drawable.clip1,
     32                 R.drawable.clip2, 
     33                 R.drawable.clip3, 
     34                 R.drawable.clip4,
     35                 R.drawable.clip5,
     36                 R.drawable.clip6, 
     37                 R.drawable.clip7,
     38                 R.drawable.clip8, 
     39                 R.drawable.clip9, 
     40                 R.drawable.clip10,
     41                 R.drawable.clip11,
     42                 R.drawable.clip12, 
     43                 R.drawable.clip13, 
     44                 R.drawable.clip14, 
     45                 R.drawable.clip15, 
     46                 R.drawable.clip16, 
     47                 R.drawable.clip17,
     48                 R.drawable.clip18, 
     49                 R.drawable.clip19, 
     50                 R.drawable.clip20,
     51                 R.drawable.clip21,
     52                 R.drawable.clip22, 
     53                 R.drawable.clip23, 
     54                 R.drawable.clip24, 
     55                 R.drawable.clip25, 
     56                 R.drawable.clip26, 
     57                 R.drawable.clip27,
     58                 R.drawable.clip28
     59         };
     60         
     61         public ImageAdapter(Context c){
     62             mContext = c;
     63         }
     64 
     65         // 获取图片的个数
     66         public int getCount(){
     67             return mImageIds.length;
     68         }
     69 
     70         // 获取图片在库中的位置
     71         public Object getItem(int position){
     72             return position;
     73         }
     74 
     75 
     76         // 获取图片ID
     77         public long getItemId(int position){
     78             return position;
     79         }
     80 
     81         public View getView(int position, View convertView, ViewGroup parent)
     82         {
     83             ImageView imageView;
     84             if (convertView == null){
     85                 // 给ImageView设置资源
     86                 imageView = new ImageView(mContext);
     87                 // 设置布局 图片120×120显示
     88                 imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
     89                 // 设置显示比例类型
     90                 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
     91             }else
     92             {
     93                 imageView = (ImageView) convertView;
     94             }
     95 
     96             imageView.setImageResource(mImageIds[position]);
     97             return imageView;
     98         }
     99     }
    100 }

    跳转界面,显示图标界面

    1 Intent intent = new Intent(MiniNoteConfige.this, IconDialog.class);  
    2 startActivityForResult(intent, RESULT_FOR_ICON);

    至于图标返回接收问题,我还没有找到更好的方法,这里就不写了

  • 相关阅读:
    Windows 科研软件推荐
    有关Python 包 (package) 的基本知识
    《Using Python to Access Web Data》Week4 Programs that Surf the Web 课堂笔记
    Coursera助学金申请模板
    《Using Databases with Python》 Week2 Basic Structured Query Language 课堂笔记
    Jupyter 解决单个变量输出问题
    解决 pandas 中打印 DataFrame 行列显示不全的问题
    《Using Python to Access Web Data》 Week3 Networks and Sockets 课堂笔记
    缓存击穿及解决方案
    jvm垃圾收集器
  • 原文地址:https://www.cnblogs.com/zjqlogs/p/2832519.html
Copyright © 2011-2022 走看看