第14天Tabhost了解
第14天Tabhost了解
Tabhost
一.效果:和TabLayout的效果一样,但是已经过时,简单了解
二.思路
方式一: 继承 Activity
布局页面中:
1, 跟标签 : <TabHost/> id 任意指定
2, <TabWidget/> id设置: @android:id/tabs
3, FrameLayout id 设置: @android:id/tabcontent
方式二: 继承TabActivity
布局页面中:
1, 根标签: <TabHost/> id设置: @android:id/tabhost
2, <TabWidget/> id设置: @android:id/tabs
3, FrameLayout id 设置:@android:id/tabcontent
三.代码
(1)布局activity_main.xml:TabHost/TabWidget/FrameLayout三个标签必须有
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@android:id/tabs"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent"
>
<!--第一个页面-->
<LinearLayout
android:id="@+id/layout_01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一个页面"
/>
</LinearLayout>
<!--第二个页面-->
<LinearLayout
android:id="@+id/layout_02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二个页面"
/>
</LinearLayout>
<!--第三个页面-->
<LinearLayout
android:id="@+id/layout_03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第三个页面"
/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
(3)Java代码:MainActivity.java
//TODO 1, Activity 继承TabActivity
public class MainActivity extends TabActivity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TODO 2, 获取TabHost
mTabHost = getTabHost();
//TODO 3, 添加选项卡
mTabHost.addTab(mTabHost.newTabSpec("tag1")//设置标签
.setIndicator("新闻")//设置标题
.setContent(R.id.layout_01)//设置显示的内容
);
mTabHost.addTab(mTabHost.newTabSpec("tag2")//设置标签
.setIndicator("娱乐")//设置标题
.setContent(R.id.layout_02)//设置显示的内容
);
mTabHost.addTab(mTabHost.newTabSpec("tag3")//设置标签
.setIndicator("体育")//设置标题
.setContent(R.id.layout_03)//设置显示的内容
);
}
}