TabHost
2024-09-04 05:14:37
(一)
知识点:id使用系统自带
1.效果图:
2.布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--选项卡标题表-->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<!--选项卡布局-->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout> </TabHost>
也可以在 activity_main.xml布局中使用include
tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tab01">
<TextView
android:text="LinnerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout>
tab2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab02">
<TextView
android:text="RelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
tab3.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab03">
<TextView
android:text="AbsoluteLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</AbsoluteLayout>
2.MainActivity.java
package com.example.administrator.hello2; import android.app.TabActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost; public class MainActivity extends TabActivity { private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tabHost = getTabHost(); LayoutInflater.from(MainActivity.this).inflate(R.layout.tab1,tabHost.getTabContentView(),true);
LayoutInflater.from(MainActivity.this).inflate(R.layout.tab2,tabHost.getTabContentView(),true);
LayoutInflater.from(MainActivity.this).inflate(R.layout.tab3,tabHost.getTabContentView(),true); tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator("线性布局").setContent(R.id.tab01));
tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator("相对布局").setContent(R.id.tab02));
tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator("绝对布局").setContent(R.id.tab03)); }
}
最新文章
- AngularJS的学习--TodoMVC的分析
- cocos2dx-3.x 导出自定义类到 lua 过程详解
- Nested Class Templates
- 打包静态库.a文件的方法(ar,ranlib,nm命令介绍)
- 【Python】回文
- Go语言-通道类型
- UNIX网络编程——epoll的 et,lt关注点
- 类Objects
- ubuntu“少折腾”
- mvc partialView+kendo window
- python学习笔记---文件的操作
- bash内置命令的特殊性,后台任务的";本质";
- ubuntu kylin18 安装NVIDIA驱动
- Daily record-September
- 《TCP/IP 详解 卷1:协议》第 3 章:链路层
- ActiveMQ broker解析
- Stay hungry, Stay foolish 的原义
- 网络请求 get 请求时, 如果参数中的字符带有+号
- Android 自定义注解(Annotation)
- 一些实用的adb命令