亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

2011.09.14(3)——— android 自定義tabhost

系統 2182 0
2011.09.14(3)——— android 自定義tabhost的tabs
參考: http://www.cnblogs.com/over140/archive/2011/03/02/1968042.html
    http://www.iteye.com/topic/1116261
  


我們直接用系統的tabhost時 如下圖
2011.09.14(3)——— android 自定義tabhost的tabs

可以看見 兩個tab中間有空隙 也許我們不需要這些空隙或者系統的樣式 但是沒有相關的xml屬性來修改 所以我們可以自定義tabs
效果如下圖 可以看見 沒有了中間的空隙



我們用單選按鈕來實現tabs


1、看布局文件

    <?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@android:id/tabhost"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:background = "#d7d7d7">
	<LinearLayout android:orientation="vertical"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent">
		<FrameLayout android:id="@android:id/tabcontent"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:layout_weight = "1">			
		</FrameLayout>	
		
		<TabWidget android:id="@android:id/tabs"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:visibility = "gone"/>
		 <RadioGroup android:gravity="right" 
		 			android:layout_gravity="bottom" 
		 			android:orientation="horizontal" 
		 			android:id="@+id/main_radio"		 			 
		 			android:layout_width="fill_parent" 
		 			android:layout_height="wrap_content">
		 	
		 	<RadioButton 
            	android:id="@+id/radio_contact" 
            	android:layout_marginTop="2.0dip"
            	android:background = "@drawable/tabcontact"
            	android:layout_width="wrap_content" 
		 		android:layout_height="fill_parent"
		 		android:button= "@null"      
            	/>
            <RadioButton android:checked="true"            	 
            	android:id="@+id/radio_session" 
            	android:layout_marginTop="2.0dip"
            	android:background = "@drawable/tabsession"
            	android:layout_width="wrap_content" 
		 		android:layout_height="fill_parent"
		 		android:button= "@null"/>  
		 	<RadioButton 
            	android:id="@+id/radio_setting" 
            	android:layout_marginTop="2.0dip"
            	android:background = "@drawable/tabsetting"
            	android:layout_width="wrap_content" 
		 		android:layout_height="fill_parent"
		 		android:button= "@null"
            	/>                               
        </RadioGroup>			
	</LinearLayout>		
</TabHost>


  


關鍵 在于 TabWidget里面
    android:visibility = "gone"
  


2、代碼

主要是三個方法:

初始化Tabhost
    private void initTabHost() {
		mTabHost = getTabHost();
		mTabHost.addTab(mTabHost.newTabSpec("tab session").setIndicator(
				"").setContent(new Intent(this,SessionListActivity.class)));
		mTabHost.addTab(mTabHost.newTabSpec("tab contact").setIndicator(
				"").setContent(new Intent(this,ContactListActivity.class)));
		mTabHost.addTab(mTabHost.newTabSpec("tab setting").setIndicator(
				"").setContent(new Intent(this,UserSettingActivitiy.class)));
		
		mTabHost.setCurrentTabByTag(_contactListTag);
		mTabHost.setCurrentTabByTag(_settingTag);
		mTabHost.setCurrentTabByTag(_sessionListTag);
		
	}
  


初始化RadioButton
    private void initTabWidget() {
	     ((RadioButton) findViewById(R.id.radio_session)).setOnCheckedChangeListener(this);
         ((RadioButton) findViewById(R.id.radio_contact)).setOnCheckedChangeListener(this);
         ((RadioButton) findViewById(R.id.radio_setting)).setOnCheckedChangeListener(this);
	}
  


設置切換事件
??
     @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            switch (buttonView.getId()) {
            case R.id.radio_contact:
                this.mTabHost.setCurrentTabByTag(_contactListTag);
                break;
            case R.id.radio_session:
                this.mTabHost.setCurrentTabByTag(_sessionListTag);
                break;
            case R.id.radio_setting:
            	this.mTabHost.setCurrentTabByTag(_settingTag);
                break;            
            }
        }
    }
  



1.  由于TabWidget被隱藏,所以相關的事件也會無效,這里取巧用RadioGroup與RadioButton的特性來處理切換,然后監聽事件調用setCurrentTabByTag來切換Activity。
2.  注意即使TabWidget被隱藏,也要為其設置indicator,否則會保持。



2011.09.14(3)——— android 自定義tabhost的tabs


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲精品一级一区二区三区 | 一级做受视频免费是看美女 | 久久er热这里只有精品免费 | 国产一区二区在免费观看 | 国产影片中文字幕 | 黄视频在线观看www 黄视频在线观看网站 | 九操网 | 久久精选 | 亚洲黄色在线观看视频 | 欧美美女啪啪 | 久久青草91线频免费观看 | 国产精品剧情原创麻豆国产 | 久操热 | 五月天婷婷免费视频 | 国产欧美一区二区精品性色99 | 夜福利视频 | 中文字幕专区高清在线观看 | 日本特级毛片 | 久久日韩精品中文字幕网 | 久久国产网站 | 亚洲网址在线观看 | 欧美最大成人毛片视频网站 | 亚洲国产视频在线观看 | 国产精品99在线观看 | 91视频国产一区 | 杨幂国产精品福利在线观看 | 99视频在线免费观看 | 国产精品福利视频 | 香蕉成人国产精品免费看网站 | 奇米影视77 | 最新国产精品精品视频 | 久久新网址| 亚洲永久精品ww47 | 国产视频一 | 九月婷婷天天澡天天添天天爽 | 99精品视频不卡在线观看免费 | 久久网综合 | 亚洲在线视频观看 | 99v视频国产在线观看免费 | 婷婷国产天堂久久综合五月 | 色婷婷综合在线 |