TabHost详解
来源:互联网 发布:网络销售培训 编辑:程序博客网 时间:2024/06/08 17:19
什么是TabHost?
先来看一个图:
由图片可以看出来,TabHost是由TabWidget和FrameLayout组合而成的。也就是TabHost是容纳选项卡按钮和选项卡内容的,如果继承自TabActivity的话,则在xml中一定要引用系统自带的tabHost 的id。如果是继承自Activity的,则要用自己定义的id,并且还要tabhost.setup()运行的时候才能显示出来,否则就会崩掉。
TabWidget是由选项卡按钮组成的,每个选项卡按钮是由文本或者图标组成的,在xml中一定要引用系统自带的id。
FrameLayout是显示内容的,它也要引用系统自带的id。
TabHost中的有一个addTab()的方法。
根据下面的例子会有更好的理解
点击Table2里面的添加选项卡自动添加Table3
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabHost android:id="@+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" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="HELLO" android:textSize="25sp" /> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#8BC34A" android:onClick="addTab" android:text="添加选项卡" android:textColor="#fff" android:textSize="20sp" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost></LinearLayout>
test.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:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:layout_gravity="center"/></LinearLayout>
MainActivity.java
package com.example.tabhost01;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class MainActivity extends Activity { private TabHost tabHost; private Button add; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabHost = (TabHost) findViewById(R.id.tabhost);// 找到控件 tabHost.setup(); TabSpec tabSpec = tabHost.newTabSpec("Label"); tabSpec.setIndicator("Table1");// 设置指示器 tabSpec.setContent(R.id.tab1);// 设置内容 tabHost.addTab(tabSpec);// 添加选项卡 tabHost.addTab(tabHost .newTabSpec("Label") .setIndicator("Table2") .setContent(R.id.tab2)); add = (Button) findViewById(R.id.add); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { tabHost.addTab(tabHost.newTabSpec("Label") .setIndicator("Table3") .setContent(new TabHost.TabContentFactory() { @Override public View createTabContent(String arg0) { View v = getLayoutInflater().from( MainActivity.this).inflate( R.layout.test, null); return v; } })); } }); }}
0 0
- TabHost详解
- TabHost详解
- TabHost详解
- TabHost详解
- TabHost详解
- TabHost详解
- TabHost详解
- TabHost详解
- TabHost详解
- Android TabHost用法详解
- Tabhost 运用详解
- TabHost使用详解
- TabHost详解0
- Android TabHost用法详解
- tabhost入门详解
- Android TabHost用法详解
- Android之TabHost用法详解
- Android控件中TabHost详解
- UVALive 7261(二分)
- SecureCRT中的实用配置
- Coverity代码静态检测工具
- 【JVM】Java内存模型
- 链表常见操作java实现一:链表初始化,求链表长度
- TabHost详解
- Linux下进程间通信--消息队列
- 分别用C#和Java实现MD5加密
- CSS3属性之多栏布局column
- 1047. Student List for Course (25)-PAT甲级真题
- shell脚本命令学习笔记
- 算法分析及实例解析(二)——贪心算法、动态规划
- 基本数据结构-队列
- web前端面试题总结