• Android——网格视图 GridView


    activity_activitygrid.xml
    <?xml version="1.0" encoding="utf-8"?>
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gdvw_1"
        android:numColumns="4"
        android:verticalSpacing="30dp"
        android:horizontalSpacing="30dp"
        android:columnWidth="120dp"
        android:gravity="center"
        android:stretchMode="columnWidth">
    <!--numColumns:列数
        verticalSpacing:垂直间距
        horizontalSpacing:水平间距
        columnWidth:列宽
        stretchMode:元素缩放模式-->
    </GridView>

    Activitygrid.java

    显示内容都为图片,未用xml文件在外加载组件,直接在java代码里面写的

    package com.example.chenshuai.test321;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class Activitygrid extends AppCompatActivity {
    
        //只需要获得图片的id
        int[] imgid;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_activitygrid);
    
            GridView gdvw_1 = (GridView)findViewById(R.id.gdvw_1);
    
            imgid=new int[]{R.drawable.anniu3,R.drawable.anniu4,R.drawable.anniu5,R.drawable.anniu6,
                            R.drawable.anniu7,R.drawable.anniu8,R.drawable.anniu9,R.drawable.anniu10,
                            R.drawable.anniu11,R.drawable.anniu12};
    
            gdvw_1.setAdapter(new MybaseAdapter());
    
            gdvw_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
                    Toast.makeText(Activitygrid.this, "position= "+position, Toast.LENGTH_SHORT).show();
                }
            });
        }
    
        class MybaseAdapter extends BaseAdapter{
            @Override
            public int getCount() {
                return imgid.length;
            }
    
            @Override
            public Object getItem(int position) {
                return imgid[position];
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
    
                //通过position  获取数据
                int imageid = imgid[position];
    
                ImageView imageView_1=null;
                //不用加载layout文件的方法
                //只需要图片视图 直接构造视图
    
                if (convertView == null)
                {
                    imageView_1 = new ImageView(Activitygrid.this);
                    imageView_1.setLayoutParams(new GridView.LayoutParams(150, 150));
                    imageView_1.setScaleType(ImageView.ScaleType.FIT_XY);
                }
                else
                {
                    imageView_1 = (ImageView)convertView;
                }
    
                imageView_1.setImageResource(imageid);
                return imageView_1;
            }
        }
    }

  • 相关阅读:
    批量导入
    循环语句
    判断循环常见
    常见C运算符
    oc将字符串中单词按照出现次数(次数都不一样)降序排序,排序之后单词只出现一次,源字符串中单词用下划线连接,生成字符串也用下滑线连接
    把字符串中的字母大小写反转OC
    查找子串出现次数OC
    现有一个数组NSMutableArray, 数组有若干个NSString的元素,进行选择法排序
    终端的一些命令
    编程语言名字来历
  • 原文地址:https://www.cnblogs.com/Chenshuai7/p/5365228.html
Copyright © 2020-2023  润新知