`
iaiai
  • 浏览: 2150381 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android 使用Gallery实现Tab

 
阅读更多
main.xml布局文件:
<?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">
	<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:layout_marginTop="30dp"
		android:unselectedAlpha="1" android:spacing="1dip" />
</LinearLayout>


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


values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <string name="hello">Hello World, Date!</string>  
    <string name="app_name">丸子-Widget</string>  
</resources>  


drawable/tab_button_select.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
	<gradient android:startColor="#FF1B1B1B" android:endColor="#FF969696"
		android:angle="90.0">
	</gradient>
</shape>


drawable/tab_button_unselect.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
	<gradient android:startColor="#FF000000" android:endColor="#FF474747"
		android:angle="90.0">
	</gradient>
</shape>


IaiaiActivity.java类:
package com.iaiai.activity;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
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.TextView;

/**
 * 
 * <p>
 * Title: IaiaiActivity.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-6-26
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class IaiaiActivity extends Activity {

	private Gallery gallery;
	private TabAdapter textAdapter;

	private static final String[] PROGRAM_NAMES = { "中央一台", "中央二台", "中央三台",
			"中央四台", "中央五台", "中央六台", "中央七台", "中央八台", };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		gallery = (Gallery) findViewById(R.id.gallery);
		textAdapter = new TabAdapter(this, Arrays.asList(PROGRAM_NAMES));
		gallery.setAdapter(textAdapter);
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				TabAdapter adapter = (TabAdapter) parent.getAdapter();
				adapter.setSelectedPos(position);
			}

		});
	}

	public class TabAdapter extends BaseAdapter {
		private Context mContext;
		private List<String> mList;
		private int mSelectedPos;

		public TabAdapter(Context context, List<String> list) {
			mContext = context;
			TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
			a.recycle();
			if (list == null)
				list = Collections.emptyList();
			mList = list;
		}

		public void setSelectedPos(int pos) {
			if (pos != mSelectedPos) {
				mSelectedPos = pos;
				notifyDataSetChanged();
			}
		}

		public int getSelectedPos() {
			return mSelectedPos;
		}

		public int getCount() {
			return mList.size();
		}

		public Object getItem(int position) {
			return mList.get(position);
		}

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

		public View getView(int position, View convertView, ViewGroup parent) {
			TextView text = null;
			if (convertView == null) {
				text = new TextView(mContext);
			} else {
				text = (TextView) convertView;
			}

			text.setTextColor(Color.WHITE);
			text.setText(mList.get(position));

			text.setLayoutParams(new Gallery.LayoutParams(102, 40));
			text.setGravity(Gravity.CENTER);

			if (position == mSelectedPos)
				text.setBackgroundResource(R.drawable.tab_button_select);
			else
				text.setBackgroundResource(R.drawable.tab_button_unselect);

			return text;
		}
	}

}


运行结果:
  • 大小: 13.4 KB
分享到:
评论

相关推荐

    Android 使用Gallery_tabhost实现标签效果图源码.rar

    Android 使用Gallery_tabhost实现标签效果图源码.rar

    android gallery 导航TAB样式 非常的漂亮和实用

    声明:这也是我学习时在网上下载的,鉴于分享精神,并且觉得很不错才上传上来的。。。。。 android gallery 导航TAB样式 非常的漂亮和实用 界面设计者可以看一看

    Android Gallery+ActivityGroup实现滑动TAB独立Activity

    Gallery+ActivityGroup实现滑动tab独立的Activity

    滑动的Tab+Gif动画显示+Gallery画廊自动切换显示

    Tab的点击切换和滑动切换实现,内部添加了Gif动画的显示,GridView自定义适配器以及Gallery的3D画廊效果可自定义时间的进行切换

    android开发资料大全

    使用Gallery实现Tab 仿QQ--tab切换动画实例 Android 小项目之---猜扑克牌游戏 (附源码) fleep滑动切换tab(切换带动画) 通过SurfaceView实现像Gallery手势滑动图片效果 Android自定义Gallery,实现CoverFlow...

    Android入门到精通源代码.

    2.5 使用Eclipse开发Android应用程序 2.5.1 使用Eclipse创建Android项目 2.5.2 Eclipse中Android项目架构 2.5.3 Eclipse中Android项目的调试和运行 第3章 Android中的Activity 3.1 Activity的作用 3.2 单Activity的...

    android开发入门与实战(下)

    9.2.1 实现Android中的广播事件 9.2.2 BroadCastReceiver介绍 9.3 应用实例详解 9.3.1 程序操作演示 9.3.2 实例编程实现 9.4 本章小结 第10章 一切为用户服务——Service应用实例 10.1 认识Service 10.2 使用...

    android开发入门与实战(上)

    9.2.1 实现Android中的广播事件 9.2.2 BroadCastReceiver介绍 9.3 应用实例详解 9.3.1 程序操作演示 9.3.2 实例编程实现 9.4 本章小结 第10章 一切为用户服务——Service应用实例 10.1 认识Service 10.2 使用...

    《Google Android开发入门与实战》.pdf

     本书内容上涵盖了用android开发的大部分场景,从android基础介绍、环境搭建、sdk介绍、market使用,到应用剖析、组件介绍、实例演示等方面。从技术实现上,讲解了5个android平台下的完整综合实例及源代码分析,...

    疯狂Android讲义源码

     5.3 使用Intent创建Tab页面 211  5.4 本章小结 212  第6章 Android应用的资源 213  6.1 资源的类型及存储方式 214  6.1.1 资源的类型以及存储方式 214  6.1.2 使用资源 216  6.2 使用字符串、颜色、  尺寸...

    Android---UI篇

    •Android---UI篇---Tab Layout(选项卡布局) • •Andorid---UI篇---TableLayout(表格布局) • •Android---UI篇---RelativeLayout(相对布局) • •Android---UI篇---GridView(网格布局) • •Android---UI篇-...

    android开发demo集合

    简明、完整、全面的安卓开发demo集合,包含如下示例 1、LinearLayout Button、RadioGroup、 CheckBox ...50、Fragment + ViewPager实现tab滑动切换 51、能够显示在桌面前面的的歌词效果 52、activity切换特效

    Android实例代码

    5.3、使用Intent创建Tab页面: 第6章、Android应用的资源 6.1、资源的类型及存储方式: 6.2、使用字符串、颜色、尺寸资源: 6.3、数组资源: 6.4、使用Drawable资源:图片资源; StateListDrawable资源; ...

    Google.Android开发入门与实战

     《Android开发入门与实战》内容上涵盖了用Android开发的大部分场景,从Android基础介绍、环境搭建、SDK介绍、Market使用,到应用剖析、组件介绍、实例演示等方面。从技术实现上,讲解了5个Android平台下的完整综合...

    疯狂Android讲义(第2版)源代码 第6章~第9章

    5.3、使用Intent创建Tab页面: 第6章、Android应用的资源 6.1、资源的类型及存储方式: 6.2、使用字符串、颜色、尺寸资源: 6.3、数组资源: 6.4、使用Drawable资源:图片资源; StateListDrawable资源; ...

    Google Android开发入门与实战的代码

    9.2.1 实现Android中的广播事件 180 9.2.2 BroadCast Receiver介绍 181 9.3 应用实例详解 181 9.3.1 程序操作演示 182 9.3.2 实例编程实现 182 9.4 本章小结 186 第10章 一切为用户服务——...

    android初学者入门项目

    初学者必看 1、LinearLayout Button、RadioGroup、 CheckBox 2、TableLayout 3、FrameLayout 霓虹灯效果 ...50、Fragment + ViewPager实现tab滑动切换 51、能够显示在桌面前面的的歌词效果 52、activity切换特效

    疯狂Android讲义.part1

    5.3 使用Intent创建Tab页面 211 5.4 本章小结 212 第6章 Android应用的资源 213 6.1 资源的类型及存储方式 214 6.1.1 资源的类型以及存储方式 214 6.1.2 使用资源 216 6.2 使用字符串、颜色、 尺寸资源 217 6.2.1 ...

Global site tag (gtag.js) - Google Analytics