码迷,mamicode.com
首页 > 其他好文 > 详细

GridView

时间:2016-05-21 11:34:14      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

GridViewActivity.java

技术分享
package com.example.lenovo.textapp4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class GridViewActivity extends AppCompatActivity {

    GridView gv_1;
    List<Integer>liv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view);

        GridView 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);

        //5-绑定
        gv_1.setAdapter(new ImageAdapter());

    }

    //2-适配器
    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(GridViewActivity.this);
            }
            //3-适配
            //转换
            ImageView imageView=(ImageView)convertView;
            imageView.setImageResource(ivid);
//            imageView.setMaxHeight(70);
//            imageView.setMaxWidth(70);

            //设置高宽
            imageView.setLayoutParams(new GridView.LayoutParams(100,100));

            //4-返回
            return imageView;
        }
    }
}
View Code

 

activity_grid_view

技术分享
<?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.example.lenovo.textapp4.GridViewActivity">

    <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">

    </GridView>

</LinearLayout>
View Code

 

GridView

标签:

原文地址:http://www.cnblogs.com/1ming/p/5514207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!