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;
- }
- }
- }
运行结果:
