`

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

阅读更多
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时 如下图


可以看见 两个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,否则会保持。



  • 大小: 6.8 KB
  • 大小: 6 KB
分享到:
评论
6 楼 lipeng88213 2011-11-21  
gadfiring 写道
tabhost之间缝隙的问题困扰了我很旧了,博主帮了我一个大忙,万分感谢!

有用就好
5 楼 gadfiring 2011-09-24  
tabhost之间缝隙的问题困扰了我很旧了,博主帮了我一个大忙,万分感谢!
4 楼 lipeng88213 2011-09-19  
安轩之 写道
但是怎么去不掉tab下面的横线呢?麻烦告诉我一下

那个横线啊 没有啊?
3 楼 安轩之 2011-09-16  
但是怎么去不掉tab下面的横线呢?麻烦告诉我一下
2 楼 lipeng88213 2011-09-16  
安轩之 写道
你好,麻烦发我一份代码。我急用,谢谢了。QQ:375290562

代码没有 因为是在项目里面的 其实 代码基本上都在文章里面了
1 楼 安轩之 2011-09-15  
你好,麻烦发我一份代码。我急用,谢谢了。QQ:375290562

相关推荐

    android自定义tabhost

    3. **切换事件监听**:通过`TabHost.OnTabChangeListener`接口,我们可以监听用户切换Tab的操作,以便进行相应的逻辑处理,如刷新当前页面内容。 4. **嵌套Fragment**:在新的Android版本中,使用Fragment来管理每...

    android自定义tabhost样式

    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String tabId) { // 处理Tab切换的逻辑 } }); ``` 6. 使用自定义的TabHost布局 最后,我们需要在...

    安卓Android源码——ViewPager和Tabhost结合,可滑动的tabhost.rar

    这个压缩包"安卓Android源码——ViewPager和Tabhost结合,可滑动的tabhost.rar"提供了将两者结合使用的示例代码,帮助开发者创建一个可以滑动的`TabHost`,增强用户体验。 `ViewPager` 是Android SDK中的一个强大...

    android自定义TabHost

    - 在自定义TabIndicator时,别忘了将`TabHost.LayoutParams`设置为`WRAP_CONTENT`,以适应不同长度的标签文本。 - 自定义颜色应与应用的整体设计风格保持一致,提供良好的用户体验。 通过以上步骤,你将能够成功...

    android TabHost自定义选项卡

    android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; android:id="@android:id/tabcontent" android:layout_width="match_parent" android:...

    安卓Android源码——ViewPager和Tabhost结合,可滑动的tabhost.zip

    在某些场景下,开发者可能希望将这两者结合,实现可滑动的标签页,这正是“安卓Android源码——ViewPager和Tabhost结合,可滑动的tabhost.zip”所展示的内容。 `ViewPager` 是Android Support Library中的一个组件...

    新浪微博布局学习——妙用TabHost.zip

    在“新浪微博布局学习——妙用TabHost.doc”文档中,可能详细介绍了如何配置和使用TabHost,包括如何自定义标签样式、处理点击事件、以及如何与其他组件如ViewPager结合等。通过学习这份资料,开发者可以深入理解...

    android--自定义TabHost

    下面我们将详细探讨如何自定义Android的TabHost,并通过具体的实现案例进行讲解。 首先,理解TabHost的基本结构是关键。TabHost通常由两部分组成:TabWidget和FrameLayout。TabWidget用于显示和管理各个标签,而...

    Android源码——android tabhost --android UI源码.zip

    3. **设置宿主**:调用`tabHost.setup();`,这使得TabHost能够管理其内容区域。 4. **添加标签**:对于每个标签,调用`TabSpec tabSpec = tabHost.newTabSpec("tag");`创建一个TabSpec,然后设置标签文本和图标,...

    自定义TabHost最简Demo源代码

    android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom"/&gt; &lt;/TabHost&gt; ``` 3. **添加图标和文字** 在传统的`TabHost...

    android使用了自定义button自定义tabHost切换页面Demo

    在Android开发中,自定义组件是提升应用独特性和用户体验的关键技术之一。本示例主要讲解如何使用自定义的Button和TabHost来实现页面间的切换,从而创建一个具有个性化标签导航功能的应用。 首先,我们来看看...

    android的自定义TabHost

    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String tabId) { // 在这里处理标签切换的逻辑 } }); ``` 5. **SinaWeibo案例** 压缩包中的...

    安卓Android源码——嵌套TabHost示例.zip

    `TabWidget`的`android:id`应设为`@android:id/tabs`,`FrameLayout`的`android:id`设为`@android:id/tabcontent`。在Java代码中,我们需要找到这两个组件并将其关联到`TabHost`上。 为了实现动态加载和切换`...

    自定义TabHost实现

    在Android开发中,`TabHost`是一个非常重要的组件,它用于创建具有多个“标签”(tabs)的应用界面,每个标签代表一个不同的活动或视图。`TabHost`提供了用户友好的交互方式,允许用户通过点击标签在不同内容之间...

    安卓Andriod源码——ViewPager和Tabhost结合,可滑动的tabhost.zip

    这个压缩包"安卓Andriod源码——ViewPager和Tabhost结合,可滑动的tabhost.zip"显然是一个示例项目,展示了如何将两者结合,创建一个可滑动的TabHost,提升用户体验。下面我们将详细讨论这两个组件以及它们的结合...

    自定义TabHost经典案例

    TabSpec spec3 = tabHost.newTabSpec("标签3"); spec3.setIndicator("标签3", getResources().getDrawable(R.drawable.ic_tab3)); spec3.setContent(R.id.tab3_content); tabHost.addTab(spec1); tabHost.addTab...

    【Android】底部、自定义Tabhost的应用

    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String tabId) { // 在这里处理Tab切换事件 } }); tabHost.setCurrentTab(0); // 设置默认选中的...

    Android-TabHost.rar_android_android tabhost_tabhost_tabhost andr

    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String tabId) { if ("tab1".equals(tabId)) { // 处理标签1被选中的逻辑 } else if ("tab2"....

    自定义tabhost

    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String tabId) { // 在这里处理切换事件 } }); ``` 总结起来,自定义TabHost主要涉及创建自定义...

Global site tag (gtag.js) - Google Analytics