TabHost详解
来源:互联网 发布:唐山军工电脑保密软件 编辑:程序博客网 时间:2024/05/29 14:50
此文转载出处:http://blog.csdn.net/harvic880925/article/details/17120325
方法一、定义tabhost:不用继承TabActivity
1、布局文件: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" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <TabHost android:id="@+id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content"> <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" > <!-- 第一个tab的布局 --> <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="林炳东" /> </LinearLayout> <!-- 第二个tab的布局 --> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="张小媛" /> </LinearLayout> <!-- 第三个tab的布局 --> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="马贝贝" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
2、JAVA代码
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabHost th=(TabHost)findViewById(R.id.tabhost); th.setup(); //初始化TabHost容器 //在TabHost创建标签,然后设置:标题/图标/标签页布局 th.addTab(th.newTabSpec("tab1").setIndicator("标签1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1)); th.addTab(th.newTabSpec("tab2").setIndicator("标签2",null).setContent(R.id.tab2)); th.addTab(th.newTabSpec("tab3").setIndicator("标签3",null).setContent(R.id.tab3)); //上面的null可以为getResources().getDrawable(R.drawable.图片名)设置图标 } }
方法二:Tab的内容分开:不用继承TabActivity
1、第一个tab的XML布局文件,tab1.xml:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="我是标签1的内容喔" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView> </LinearLayout>
2、第二个tab的XML布局文件,tab2.xml:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="标签2" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
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详解
- [javascript] js 动态控制透明度
- C++的强制类型转换
- JDK在windows和linux中安装配置指南
- svn 冲突的产生与解决
- 解决ORA-00054和ORA-00031
- TabHost详解
- 习题5-3 卡片游戏 UVa10935
- [数据结构]双向链表的前插操作
- Listview与Button点击事件冲突看安卓点击事件分发机制
- Oracle DISTINCT 关键字
- Presenting action sheet clipped by its superview
- 其实我们可以做自己的心理医生
- JBPM及J2EE各种jar包的作用
- Oracle删除重复记录只保留一条数据的几种方法