zoukankan      html  css  js  c++  java
  • Android——GridView

    layout文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.hanqi.testapp2.TestActivity9">
    11 
    12     <GridView
    13         android:layout_width="match_parent"
    14         android:layout_height="match_parent"
    15         android:id="@+id/gv_1"
    16         android:numColumns="4"
    17         android:stretchMode="columnWidth"
    18         android:gravity="center"
    19         android:horizontalSpacing="30dp"></GridView>
    20 </LinearLayout>

    java类:

     1 package com.hanqi.testapp2;
     2 
     3 import android.os.Bundle;
     4 import android.support.v7.app.AppCompatActivity;
     5 import android.view.View;
     6 import android.view.ViewGroup;
     7 import android.widget.BaseAdapter;
     8 import android.widget.GridView;
     9 import android.widget.ImageView;
    10 
    11 import java.util.ArrayList;
    12 import java.util.List;
    13 
    14 public class TestActivity9 extends AppCompatActivity {
    15 
    16     GridView gv_1;
    17     List<Integer> liv;
    18     @Override
    19     protected void onCreate(Bundle savedInstanceState) {
    20         super.onCreate(savedInstanceState);
    21         setContentView(R.layout.activity_test9);
    22         gv_1 = (GridView)findViewById(R.id.gv_1);
    23 
    24         //1、数据 图片的id
    25         liv = new ArrayList<>();
    26         liv.add(R.drawable.f1);
    27         liv.add(R.drawable.f2);
    28         liv.add(R.drawable.f3);
    29         liv.add(R.drawable.f4);
    30         liv.add(R.drawable.f5);
    31         liv.add(R.drawable.f6);
    32         liv.add(R.drawable.f7);
    33         liv.add(R.drawable.f8);
    34         gv_1.setAdapter(new ImageAdapter());
    35     }
    36     //适配器
    37     class ImageAdapter extends BaseAdapter
    38     {
    39         @Override
    40         public int getCount() {
    41             return liv.size();
    42         }
    43 
    44         @Override
    45         public Object getItem(int position) {
    46             return liv.get(position);
    47         }
    48 
    49         @Override
    50         public long getItemId(int position) {
    51             return 0;
    52         }
    53 
    54         @Override
    55         public View getView(int position, View convertView, ViewGroup parent) {
    56             //1.得到数据 图片的id
    57             Integer ivid = liv.get(position);
    58             //2.准备View
    59             if (convertView ==null) {
    60                 convertView = new ImageView(TestActivity9.this);
    61             }
    62             //3.适配
    63             //转换
    64             ImageView imageView = (ImageView)convertView;
    65             imageView.setImageResource(ivid);
    66 
    67             //imageView.setMaxHeight(70);
    68             //imageView.setMaxWidth(70);
    69 
    70             imageView.setLayoutParams(new GridView.LayoutParams(110, 110));
    71 
    72             return imageView;
    73         }
    74     }
    75 }

    效果图为:

  • 相关阅读:
    TabControl 切换 内嵌web页面直接响应滚动事件
    进程、应用程序域和对象上下文
    CSharp中的多线程——线程同步基础
    CSharp中的多线程——入门
    注重实效的程序员之快速参考指南
    学习语言技术快速入门——五步骤
    利用jQuery选择将被操作的元素
    CSharp中的多线程——使用多线程
    android开发文件介绍
    三角函数公式
  • 原文地址:https://www.cnblogs.com/hanazawalove/p/5513209.html
Copyright © 2011-2022 走看看