android 滑动tabhost,Android TabHost中实现标签的滚动

这篇博客展示了如何在Android中使用TabHost和ViewPager结合RadioButton来创建类似QQ的效果,通过代码详细解释了如何设置TabHost的内容,动态添加Tab,并通过手势切换不同Activity。同时,还提及了TabHost的onTabChanged方法用于处理选中状态的改变。
摘要由CSDN通过智能技术生成

非常漂亮的一段代码:

public class TabhostTestActivity extends TabActivity implements

TabContentFactory{

private String[] lables = new String[] { "首页", "消息", "朋友", "更多" };

private int[] icons = new int[] { R.drawable.ic_home, R.drawable.ic_msg, R.drawable.ic_friend, R.drawable.ic_more };

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.tabhost);

TabHost th = getTabHost();

tabHost = getTabHost();

for (int i = 0; i < lables.length; i++) {

View view = LayoutInflater.from(this).inflate(R.layout.menu, null);

ImageView menu_ic = (ImageView) view.findViewById(R.id.menu_ic);

menu_ic.setImageResource(icons[i]);

TextView text = (TextView) view.findViewById(R.id.menu_lable);

text.setText(lables[i]);

tabHost.addTab(tabHost.newTabSpec(String.valueOf(i)).setIndicator(view).setContent(this));

}

tabHost.setCurrentTab(0);   }

@Override

public void onTabChanged(String tabId) {

// tabId 为newTabSpec(String tag) 中传入的字符串tag,这里tag是0,1,2,3 可以转换为整形便于判断

int tabID = Integer.valueOf(tabId);

for (int i = 0; i < mTabWidget.getChildCount(); i++) {

if (i == tabID) {

mTabWidget.getChildAt(i).setBackgroundResource(

R.drawable.indicator_selected);

} else {

mTabWidget.getChildAt(i).setBackgroundResource(

R.drawable.indicator_unselected);

}

}

}

TabHost通过手势切换Activity

2.ViewPager+RadioButton仿QQ效果

原理说的很明白。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值