Android组件的使用:Gallery

Android中Gallery(画廊)的使用

1、Gallery


1.1、使用BaseAdapter完成Gallery

思路:通过BaseAdapter类加载一个布局文件信息。把一个布局问价转换成一个View,加载到Gallery中。
步骤:-->新建一个布局文件gallery_layout.xml,其中只有一个Gallery组件。
           -->新建一个布局文件gallery_item_layout.xml,其中只有一个ImageView组件。
           -->新建一个GalleryDemoActivity类,此类继承自Activity。
           -->新建一个GalleryDemoAdapter类,此类继承自BaseAdapter。
1)、建立gallery_layout.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/gly_showglyimg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:spacing="2dp" />

</RelativeLayout>
2)、建立gallery_item_layout.xml,其中只有一个ImageView组件,该组件用于显示图片。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
	<ImageView 
	    android:id="@+id/iv_showglyimg"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:src="@drawable/ic_launcher"/>
</LinearLayout>
3)、新建一个GalleryDemoAdapter,继承自BaseAdapter类,并覆写相应方法。
package com.example.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.example.highcomponent.R;

public class GalleryDemoAdapter extends Ba
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值