• 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;
            }
        }
    }

  • 相关阅读:
    力扣(LeetCode)验证回文字符串II 个人题解
    力扣(LeetCode)寻找数组的中心索引 个人题解
    力扣(LeetCode)验证回文串 个人题解
    力扣(LeetCode)三个数的最大乘积 个人题解
    力扣(LeetCode)二进制求和 个人题解
    力扣(LeetCode)加一 个人题解
    力扣(LeetCode)整数反转 个人题解
    力扣(LeetCode)颠倒二进制位 个人题解
    力扣(LeetCode)最后一个单词的长度 个人题解
    力扣(LeetCode)学生出勤记录I 个人题解
  • 原文地址:https://www.cnblogs.com/Chenshuai7/p/5365228.html
Copyright © 2020-2023  润新知