Android 組件Gallery和GridView示例講解
Android Gallery和GridView組件:
Gallery 畫廊
Gallery是一個(gè)內(nèi)部元素可以水平滾動(dòng),并且可以把當(dāng)前選擇的子元素定位在它中心的布局組件。
我們還是直接看看例子的運(yùn)行效果。

下面上代碼,相關(guān)解釋都放在代碼里了。
1、建立一個(gè)新項(xiàng)目 HelloGallery
2、拷貝wallpaper_0.jpg…wallpaper_9.jpg 10個(gè)圖片文件到res/drawable目錄
3、res/layout/main.xml文件的內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?> <framelayout android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/FrameLayout01"> <imageview android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/ImageView01" android:src="@drawable/wallpaper_0"> </imageview> <gallery android:layout_height="wrap_content" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Gallery01" android:spacing="5dp"> </gallery> </framelayout>
其中我們使用FrameLayout來實(shí)現(xiàn)疊加效果,使用ImageView來顯示大圖,Gallery來展示畫廊,android:spacing="5dp" 屬性則是用來設(shè)置元素之間的間隔。
4、在res/values/目錄中新建一個(gè)attrs.xml內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare -styleable="" name="HelloGallery">
<attr name="android:galleryItemBackground">
</attr></declare>
</resources>
5、在MainHelloGallery.java中的內(nèi)容如下:
package android.basic.lesson13;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class MainHelloGallery extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//定義UI組件
final ImageView iv= (ImageView)findViewById(R.id.ImageView01);
Gallery g = (Gallery) findViewById(R.id.Gallery01);
//設(shè)置圖片匹配器
g.setAdapter(new ImageAdapter(this));
//設(shè)置AdapterView點(diǎn)擊監(jiān)聽器,Gallery是AdapterView的子類
g.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//顯示點(diǎn)擊的是第幾張圖片
Toast.makeText(MainHelloGallery.this, "" + position,
Toast.LENGTH_LONG).show();
//設(shè)置背景部分的ImageView顯示當(dāng)前Item的圖片
iv.setImageResource(((ImageView)view).getId());
}
});
}
//定義繼承BaseAdapter的匹配器
public class ImageAdapter extends BaseAdapter {
//Item的修飾背景
int mGalleryItemBackground;
//上下文對(duì)象
private Context mContext;
//圖片數(shù)組
private Integer[] mImageIds = { R.drawable.wallpaper_0,
R.drawable.wallpaper_1, R.drawable.wallpaper_2,
R.drawable.wallpaper_3, R.drawable.wallpaper_4,
R.drawable.wallpaper_5, R.drawable.wallpaper_6,
R.drawable.wallpaper_7, R.drawable.wallpaper_8,
R.drawable.wallpaper_9 };
//構(gòu)造方法
public ImageAdapter(Context c){
mContext = c;
//讀取styleable資源
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
//返回項(xiàng)目數(shù)量
@Override
public int getCount() {
return mImageIds.length;
}
//返回項(xiàng)目
@Override
public Object getItem(int position) {
return position;
}
//返回項(xiàng)目Id
@Override
public long getItemId(int position) {
return position;
}
//返回視圖
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(mContext);
iv.setImageResource(mImageIds[position]);
//給生成的ImageView設(shè)置Id,不設(shè)置的話Id都是-1
iv.setId(mImageIds[position]);
iv.setLayoutParams(new Gallery.LayoutParams(120, 160));
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setBackgroundResource(mGalleryItemBackground);
return iv;
}
}
}
我們點(diǎn)擊某一張圖片,會(huì)把該子元素的圖片顯示在放在后面一層的ImageView組件中。有興趣的同學(xué)可以了解一下AdapterView的繼承關(guān)系:

以上就是對(duì)Android Gallery 和 GridView 組件的介紹,后續(xù)繼續(xù)對(duì)相關(guān)知識(shí)補(bǔ)充,謝謝大家對(duì)本站的支持!
- Android中GridView插件的使用方法
- Android控件gridview實(shí)現(xiàn)單行多列橫向滾動(dòng)效果
- Android GridView擴(kuò)展仿微信微博發(fā)圖動(dòng)態(tài)添加刪除圖片功能
- Android開發(fā)實(shí)現(xiàn)橫向列表GridView橫向滾動(dòng)的方法【附源碼下載】
- android ListView和GridView拖拽移位實(shí)現(xiàn)代碼
- Android GridView實(shí)現(xiàn)滾動(dòng)到指定位置的方法
- android中GridView的用法示例
- Android開發(fā)之使用GridView展示圖片的方法
- Android GridView仿微信朋友圈顯示圖片
- Android開發(fā)之組件GridView簡(jiǎn)單使用方法示例
相關(guān)文章
Android學(xué)習(xí)項(xiàng)目之簡(jiǎn)易版微信為例(二)
這篇文章主要以簡(jiǎn)易版微信為例,實(shí)現(xiàn)簡(jiǎn)易版微信的登陸、注冊(cè)界面的編寫與簡(jiǎn)單交互,感興趣的小伙伴們可以參考一下2016-06-06
OKhttp攔截器實(shí)現(xiàn)實(shí)踐環(huán)節(jié)源碼解析
這篇文章主要為大家介紹了OKhttp攔截器實(shí)現(xiàn)實(shí)踐環(huán)節(jié)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android?Flutter控件封裝之視頻進(jìn)度條的實(shí)現(xiàn)
這篇文章主要來和大家分享一個(gè)很簡(jiǎn)單的控制器封裝案例,包含了基本的播放暫停,全屏和退出全屏,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-06-06
RecylerView實(shí)現(xiàn)流布局StaggeredGridLayoutManager使用詳解
這篇文章主要為大家詳細(xì)介紹了RecylerView實(shí)現(xiàn)流布局StaggeredGridLayoutManager使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
基于Viewpager2實(shí)現(xiàn)登錄注冊(cè)引導(dǎo)頁面
這篇文章主要為大家詳細(xì)介紹了基于Viewpager2實(shí)現(xiàn)登錄注冊(cè)引導(dǎo)頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Android使用Intent隱式實(shí)現(xiàn)頁面跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Android使用Intent隱式來實(shí)現(xiàn)向上跳轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08

