• 网格视图


    package com.hanqi.testapp2;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestActivity10 extends AppCompatActivity {
    
        GridView gv_1;
        List<Integer> liv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test10);
            gv_1=(GridView)findViewById(R.id.gv_1);
            //1.数据 图片的id
            liv=new ArrayList<>();
            liv.add(R.drawable.f1);
            liv.add(R.drawable.f2);
            liv.add(R.drawable.f3);
            liv.add(R.drawable.f4);
            liv.add(R.drawable.f5);
            liv.add(R.drawable.f6);
            liv.add(R.drawable.f7);
            liv.add(R.drawable.f8);
            gv_1.setAdapter(new ImageAdapter());
        }
        //适配器
        class ImageAdapter extends BaseAdapter
        {
            @Override
            public int getCount() {
                return liv.size();
            }
    
            @Override
            public Object getItem(int position) {
                return liv.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return 0;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
    
                //1 得到数据  图片的id
                Integer ivid=liv.get(position);
                //2 准备View
                if (convertView==null) {
                    convertView= new ImageView(TestActivity10.this);
                }
                //3 适配
                // 转换
                ImageView imageView=(ImageView)convertView;
                imageView.setImageResource(ivid);
    //            imageView.setMaxWidth(70);
    //            imageView.setMaxHeight(70);
                imageView.setLayoutParams(new GridView.LayoutParams(110,110));
                return imageView;
            }
        }
    
    }
    View Code
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.hanqi.testapp2.TestActivity10"
        android:orientation="vertical">
        <GridView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/gv_1"
            android:numColumns="4"
            android:stretchMode="columnWidth"
            android:gravity="center"
            android:horizontalSpacing="30dp">
    
        </GridView>
    
    </LinearLayout>

  • 相关阅读:
    【转】Java学习---HashMap的工作原理
    【转】Java学习---集合框架那些事
    Linux学习---linux下的彩蛋和各种有趣的命令
    【转】VMware虚拟机三种网络模式超详解
    沃顿商学院的MBA课程
    本杰明-富兰克林的13节制
    美学需要读的书
    芒格推荐书单
    回声消除(AEC)原理
    adc0和adc1
  • 原文地址:https://www.cnblogs.com/jiang2538406936/p/5514221.html
Copyright © 2020-2023  润新知