TabHost的使用

使用tab有两种方式

1、不继承tabActivity

这种方式首先在布局中TabHost的id必须是android:id="@android:id/tabhost",TabWidget的id必须是 android:id="@android:id/tabs"

TabHost还包含FrameLayout,id也必须是android:id="@android:id/tabcontent",FrameLayout里面可以放不同的布局

在activity中,通过findviewbyid找到TabHost,在通过tabhost的setup()方法找到TabWidget  和 FrameLayot,这个方法底层实现了这种功能

最后通过addTab()方法就能实现了

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <LinearLayout
                android:id="@+id/ll1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="首页" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="第二页" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="第三页" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//找到选项卡
		TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);
		tabhost.setup();
		
		TabSpec tab1 = tabhost.newTabSpec("tab1");
		tab1.setIndicator("首页", getResources().getDrawable(R.drawable.ic_launcher));

		tab1.setContent(R.id.ll1);
		tabhost.addTab(tab1);
		
		TabSpec tab2 = tabhost.newTabSpec("tab2");
		tab2.setIndicator("第二页", getResources().getDrawable(R.drawable.ic_launcher));//指定头
		tab2.setContent(R.id.ll2);//指定内容
		tabhost.addTab(tab2);
		
		TabSpec tab3 = tabhost.newTabSpec("tab3");
		tab3.setIndicator("第三页", getResources().getDrawable(R.drawable.ic_launcher));
		tab3.setContent(R.id.ll3);
		tabhost.addTab(tab3);
	}


}
2、继承TabActivity

这种方式就不用setcontentview找到布局,也就是不用findviewbyid找到tabhost,只需要通过TabHost tabhost = getTabHost();就可以找打tabhost

注意这连个方法的参数

// tab1.setContent(viewId); 通过id找到tabhost的内容
// tab1.setContent(intent);通过intent找到tabhost的内容,这时候就需要新建activity作为内容,这种方式方便,可以重新用布局来定义tabhost的内容,开发中常见
// tab1.setIndicator(view); 这种方式通过一个view来指定头显示,可以自定义view
// tab1.setIndicator(label, icon); 这种方式是通过文字和图片来显示头

public class MainActivity2 extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        TabHost tabhost = getTabHost();
        
        TabSpec tab1 = tabhost.newTabSpec("tab1");
        tab1.setIndicator(createView("首页"));//这里传递一个view,可自定义
        tab1.setContent(new Intent(this,Activity1.class));
        tabhost.addTab(tab1);
        
        TabSpec tab2 = tabhost.newTabSpec("tab2");
        tab2.setIndicator(createView("第二页"));
        tab2.setContent(new Intent(this,Activity2.class));
        tabhost.addTab(tab2);
        
        TabSpec tab3 = tabhost.newTabSpec("tab3");
        tab3.setIndicator(createView("第三页"));
        tab3.setContent(new Intent(this,Activity3.class));
        tabhost.addTab(tab3);
    }
    
    public View createView(String name){
    	View view = getLayoutInflater().inflate(R.layout.header, null);
    	TextView tv = (TextView) view.findViewById(R.id.tv_info);
    	tv.setText(name);
    	return view;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TabHostAndroid 中的一个容器,用于实现多个标签页之间的切换。下面是 TabHost使用方法详解: 1. 在 XML 布局文件中添加 TabHost 控件: ```xml <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> </TabHost> ``` 2. 在 Activity 中初始化 TabHost: ```java TabHost tabHost = findViewById(android.R.id.tabhost); tabHost.setup(); ``` 3. 添加标签页: ```java TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("Tab1"); tabSpec1.setIndicator("Tab 1"); tabSpec1.setContent(R.id.tab1_content); TabHost.TabSpec tabSpec2 = tabHost.newTabSpec("Tab2"); tabSpec2.setIndicator("Tab 2"); tabSpec2.setContent(R.id.tab2_content); tabHost.addTab(tabSpec1); tabHost.addTab(tabSpec2); ``` 4. 创建标签页的布局: 在布局文件中创建用于显示每个标签页内容的 View,例如: ```xml <LinearLayout android:id="@+id/tab1_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 1 的内容 --> </LinearLayout> <LinearLayout android:id="@+id/tab2_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 2 的内容 --> </LinearLayout> ``` 以上就是 TabHost 的基本使用方法。通过添加不同的标签页和对应的布局,可以实现多个标签页之间的切换和显示不同的内容。你可以根据自己的需求来定制标签页的样式和内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值