Android 使用Gallery实现Tab
来源:互联网 发布:知乎福利大合集 编辑:程序博客网 时间:2024/05/24 15:39
main.xml布局文件:
values/attrs.xml:
values/strings.xml:
drawable/tab_button_select.xml:
drawable/tab_button_unselect.xml:
IaiaiActivity.java类:
运行结果:
- <?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;
- }
- }
- }
运行结果:
- Android 使用Gallery实现Tab
- gallery实现tab
- Android使用Gallery实现轮播图效果
- 用gallery实现可滑动的Tab
- android UI进阶之用gallery实现可滑动的Tab
- android UI进阶之用gallery实现可滑动的Tab
- android UI进阶之用gallery实现可滑动的Tab
- android UI进阶之用gallery实现可滑动的Tab
- android UI进阶之用gallery实现可滑动的Tab
- 处女男学Android(十一)---Gallery、ViewPager和ViewPager+Fragment实现的Tab导航
- android--Gallery的实现
- Android--Gallery实现切一屏
- 使用ViewPager实现gallery
- android gallery的使用
- Android : Gallery使用例子
- android:使用fragment实现tab切换
- Android开发——使用Gallery实现“多级联动”
- 使用Android自带Gallery组件实现CoverFlow,源码+解析
- Andoid2.X各字段意义
- NSDate时间操作
- 5.1学习进展
- UITextView控件的用法详解
- process bar 收集
- Android 使用Gallery实现Tab
- 匈牙利命名法(Hungarian)
- 幻灯片效果+背景音乐
- Extjs多选下拉框02
- 重学设计模式 - Abstract Factory模式
- bounds和frame的差别
- Ubuntu下ADB调试Android找不到设备的解决方法
- 安装Nginx
- Java 二进制与16进制相互转换