TabHost

(一)

知识点:id使用系统自带

1.效果图:

2.布局

activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <TabHost
 3     android:id="@android:id/tabhost"
 4     android:layout_width="match_parent"
 5     android:layout_height="wrap_content"
 6     xmlns:android="http://schemas.android.com/apk/res/android">
 7     <LinearLayout
 8         android:orientation="vertical"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content">
11         <!--选项卡标题表-->
12          <TabWidget
13              android:id="@android:id/tabs"
14              android:layout_width="match_parent"
15              android:layout_height="wrap_content"></TabWidget>
16          <!--选项卡布局-->
17         <FrameLayout
18             android:id="@android:id/tabcontent"
19             android:layout_width="match_parent"
20             android:layout_height="match_parent"></FrameLayout>
21     </LinearLayout>
22 
23 
24 </TabHost>

也可以在  activity_main.xml布局中使用include

tab1.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:id="@+id/tab01">
 6     <TextView
 7         android:text="LinnerLayout"
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content" />
10 
11 </LinearLayout>

tab2.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     xmlns:android="http://schemas.android.com/apk/res/android"
 6     android:id="@+id/tab02">
 7     <TextView
 8     android:text="RelativeLayout"
 9     android:layout_width="match_parent"
10     android:layout_height="wrap_content" />
11 </RelativeLayout>

tab3.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <AbsoluteLayout
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     xmlns:android="http://schemas.android.com/apk/res/android"
 6     android:id="@+id/tab03">
 7     <TextView
 8         android:text="AbsoluteLayout"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content" />
11 </AbsoluteLayout>

2.MainActivity.java

 1 package com.example.administrator.hello2;
 2 
 3 import android.app.TabActivity;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.LayoutInflater;
 7 import android.widget.TabHost;
 8 
 9 public class MainActivity extends TabActivity {
10 
11     private TabHost tabHost;
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16 
17         tabHost = getTabHost();
18 
19         LayoutInflater.from(MainActivity.this).inflate(R.layout.tab1,tabHost.getTabContentView(),true);
20         LayoutInflater.from(MainActivity.this).inflate(R.layout.tab2,tabHost.getTabContentView(),true);
21         LayoutInflater.from(MainActivity.this).inflate(R.layout.tab3,tabHost.getTabContentView(),true);
22 
23         tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator("线性布局").setContent(R.id.tab01));
24         tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator("相对布局").setContent(R.id.tab02));
25         tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator("绝对布局").setContent(R.id.tab03));
26 
27     }
28 }
原文地址:https://www.cnblogs.com/sunxiaoyan/p/9067589.html