android-利用gallery做导航条

以后gallery做导航条




主布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <ImageView android:id="@+id/bar_img" android:layout_width="60dip" android:layout_height="60dip" />
  <TextView android:id="@+id/bar_title" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent" />
</LinearLayout>

每一个图片项目的布局


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
    <ImageView android:id="@+id/BtnLeft" android:background="@drawable/guanggao_left" android:layout_width="30dip" android:layout_height="60dip"/>
    
<Gallery
	android:id="@+id/gg"
	android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:spacing="10dip"
    android:layout_weight = "1"
	/>
	<ImageView android:id="@+id/BtnRight" android:background="@drawable/guanggao_right" android:layout_width="30dip" android:layout_height="60dip"/>
</LinearLayout>

<ImageSwitcher 
	android:id="@+id/imageswitcher"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp" 
    />
</LinearLayout>


package cn.etouch;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery.LayoutParams;

public class GalleryActivity extends Activity
{

	private Gallery gallery;
    private ImageView btnLeft,btnRight;
	private int[] resIds=new int[] {R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,
			R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9};
    private String[] name  = {"书城","营业厅","软件","学习","主题","百合","音乐","视频","游戏"};
	//private Drawable[] resIds ;
    
    
public class ButtonAdapter extends BaseAdapter{
	LayoutInflater inflater;
	int mGalleryItemBackground;
    private Context mContext;
    Holder holder;

	public ButtonAdapter(Context c)
	{
		mContext=c;
		//TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
        //mGalleryItemBackground = typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);  
	}
	
	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		inflater = LayoutInflater.from(mContext);
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.toolbar_row, null);
			holder = new Holder();
			holder.img = (ImageView) convertView
					.findViewById(R.id.bar_img);
			holder.title = (TextView) convertView
					.findViewById(R.id.bar_title);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.img.setBackgroundResource(resIds[position % resIds.length]);
		holder.title.setText(name[position % resIds.length]);
		
		return convertView;
//		ImageView imageView = new ImageView(mContext);
//		
//        // 通过取余来循环取得resIds数组中的图像资源ID
//		imageView.setImageResource(resIds[position % resIds.length]);
//		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//		imageView.setLayoutParams(new Gallery.LayoutParams(163, 106));
//		//imageView.setBackgroundResource(mGalleryItemBackground);
//		return imageView;
	}
}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnLeft = (ImageView)findViewById(R.id.BtnLeft);
        btnRight = (ImageView)findViewById(R.id.BtnRight);
        btnLeft.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//gallery.getLeft();
				int position = gallery.getSelectedItemPosition();
				gallery.setSelection(position+1);
			}
		});
        btnRight.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//Toast.makeText(GalleryActivity.this, gallery.getNextFocusRightId(), Toast.LENGTH_SHORT).show();
				int position = gallery.getSelectedItemPosition();
				gallery.setSelection(position+1);
			}
		});
       // CutIcon cut = new CutIcon(resIds, "");
       //resIds = cut.cutIcon("50", "50);
        gallery=(Gallery)findViewById(R.id.gg);
        ButtonAdapter buttonadapter=new ButtonAdapter(this);
        gallery.setAdapter(buttonadapter);
        gallery.setBackgroundResource(R.drawable.gallerybg);
       // gallery.setOnItemSelectedListener(this);
       
        // 设置ImageSwitcher组件的工厂对象
        //imageSwitcher.setFactory(this);
        // 设置ImageSwitcher组件显示图像的动画效果
//        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
//        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));


    }
    class Holder {
    	ImageView img;
    	TextView title;
    }
	
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值