您的位置:首页 > 其它

tabhost(tabweight自定义)

2014-06-12 15:34 134 查看
main.call
public class MainActivity extends TabActivity {

private TabHost mTabHost;
private TextView tv;
private TabWidget tabWidget;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//		mTabHost=(TabHost)this.findViewById(android.R.id.tabhost);
mTabHost = getTabHost();
//		mTabHost.setup();
Intent intent1 = new Intent(this,OneActivity.class);
createTab("公共聊天室",intent1);
Intent intent2 = new Intent(this,TwoActivity.class);
createTab("周边",intent2);
Intent intent3 = new Intent(this,OneActivity.class);
createTab("历史",intent3);
mTabHost.setCurrentTab(1);

tabWidget = mTabHost.getTabWidget();
mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
for (int i =0; i < tabWidget.getChildCount(); i++) {
View vvv = tabWidget.getChildAt(i);
if(mTabHost.getCurrentTab()==i){
if(tabId=="公共聊天室"){
Toast.makeText(MainActivity.this, "公共聊天室", 1).show();
}if(tabId=="周边"){
Toast.makeText(MainActivity.this, "周边", 1).show();
}if(tabId=="历史"){
Toast.makeText(MainActivity.this, "历史", 1).show();
}
}
}
}});

}

private void createTab(String text, Intent intent1) {
mTabHost.addTab(mTabHost.newTabSpec(text)
.setIndicator(createTabView(text))   //创建自定义的tabweigth
.setContent(intent1));

}

private View createTabView(String text) {
View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null);
tv = (TextView) view.findViewById(R.id.tv_tab);
tv.setText(text);
return view;

}

}
tab_indicator.xml 自定义的tabweight界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:theme="@android:style/Theme.NoTitleBar"
android:background="@drawable/chat_tab_selector">

<TextView
android:id="@+id/tv_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab1" />
</LinearLayout>
chat_tab_selector.xml  点击tabweiget的背景色变化
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:state_focused = "true"android:drawable = "@drawable/topbar_bg_down" /><itemandroid:state_selected = "true"android:drawable = "@drawable/topbar_bg_down" /><itemandroid:state_pressed = "true"android:drawable = "@drawable/topbar_bg_down"  /><item android:drawable = "@drawable/topbar_background" /></selector>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: