gallery画廊控件

xml代码:

<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:orientation="vertical"
  
    tools:context="com.example.gallery.MainActivity$PlaceholderFragment" >

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

java代码:

package com.example.gallery;


import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.os.Build;


public class MainActivity extends ActionBarActivity {
private Gallery gallery;
private ImageAdapter imageAdapter;
private int[] resIds={R.drawable.face1,R.drawable.face2,R.drawable.face3
,R.drawable.face4,R.drawable.face5,R.drawable.face6,R.drawable.face7
,R.drawable.face8,R.drawable.face9};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
gallery=(Gallery) findViewById(R.id.gallery);
imageAdapter=new ImageAdapter(this);
gallery.setAdapter(imageAdapter);
}


public class ImageAdapter extends BaseAdapter{
private Context context;
int mGalleryItemBackground;//使用简单计数器,填充背景图片

public ImageAdapter(Context context){
this.context=context;
//读取属性
TypedArray typedArray=obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground=typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return resIds[position];
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
//自定义的适配器,需要用自定义的布局来显示,通常Android的通用布局是不能满足我们的需求
//可以手工创建一个view视图,也可以inflate填充一个XML
//从数据源中根据position获得每一个item的值,填充到制定的xml布局中
//View convertView是一个旧的布局,如果没有新的布局填充的时候,将使用旧的布局
//ViewGroup parent 当前的布局会被追加到父布局中
ImageView imageView =new ImageView(context);
imageView.setImageResource(resIds[position%resIds.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(136,138));
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}

}


}


在values包下新建个xml文件attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Gallery">
        <attr name="android:galleryItemBackground"></attr>
    </declare-styleable>
</resources>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值