tabhost(tabweight自定义)

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" >
    <item    
        android:state_focused = "true"    
        android:drawable = "@drawable/topbar_bg_down" /> 
        <item    
        android:state_selected = "true"    
        android:drawable = "@drawable/topbar_bg_down" /> 
        <item    
        android:state_pressed = "true"    
        android:drawable = "@drawable/topbar_bg_down"  />     
        <item android:drawable = "@drawable/topbar_background" />    

</selector>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值