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;
}
}

}


运行结果:
[img]http://dl.iteye.com/upload/attachment/507768/5741fd4f-1803-3efd-9781-7a643fd80b2a.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值