Android实现漂亮的Gallery画廊
程序员文章站
2023-11-05 15:16:40
本文实例为大家分享了android实现gallery画廊的具体代码,供大家参考,具体内容如下
仅是实现基本功能,页面粗糙请见谅
图片下标0开始
activity_...
本文实例为大家分享了android实现gallery画廊的具体代码,供大家参考,具体内容如下
仅是实现基本功能,页面粗糙请见谅
图片下标0开始
activity_main.xml页面:
<?xml version="1.0" encoding="utf-8"?> <relativelayout 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" tools:context="com.example.gallery.mainactivity"> <gallery android:id="@+id/galpicture" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centervertical="true" android:layout_centerhorizontal="true" /> </relativelayout>
galleryadapter.java页面:
package com.example.gallery; import android.content.context; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.gallery; import android.widget.imageview; public class galleryadapter extends baseadapter { private context mcontext; int[] images = {r.mipmap.apple, r.mipmap.banana, r.mipmap.bicycle, r.mipmap.chair,r.mipmap.chopsticks, r.mipmap.dog, r.mipmap.fish, r.mipmap.pear}; //本地图片 public galleryadapter (context context) { this.mcontext = context; } @override public int getcount() { return images.length; } @override public object getitem(int i) { return i; } @override public long getitemid(int i) { return i; } @override public view getview(int i, view view, viewgroup viewgroup) { imageview image = new imageview(mcontext); image.setimageresource(images[i]); //设置图片 image.setadjustviewbounds(true); //是否调整边框 image.setlayoutparams(new gallery.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent)); return image; } }
mainactivity.java页面:
package com.example.gallery; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.gallery; import android.widget.toast; public class mainactivity extends appcompatactivity { galleryadapter galleryadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gallery galpicture = findviewbyid(r.id.galpicture); galleryadapter = new galleryadapter(mainactivity.this); galpicture.setadapter(galleryadapter); //相应的点击事件 galpicture.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { toast.maketext(mainactivity.this, "图片" + i, toast.length_long).show(); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 姐姐和弟第