tabHost的简单使用
来源:互联网 发布:y系列6及电机数据大全 编辑:程序博客网 时间:2024/05/17 08:12
现在虽然TabHost很少用了,但很多项目仍然在用,值得了解下。
方式一:使用TabHost组件,
1.布局文件
<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="${relativePackage}.${activityClass}" >
<TabHost
android:id="@+id/tabhost1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="6" >
<!-- 第一个tab的布局 -->
<RelativeLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_centerInParent="true"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab" />
</RelativeLayout>
<!-- 第二个tab的布局 -->
<RelativeLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_centerInParent="true"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab2" />
</RelativeLayout>
<!-- 第三个tab的布局 -->
<RelativeLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_centerInParent="true"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab3" />
</RelativeLayout>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</TabWidget>
</LinearLayout>
</TabHost>
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TabHost
android:id="@+id/tabhost1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="6" >
<!-- 第一个tab的布局 -->
<RelativeLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_centerInParent="true"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab" />
</RelativeLayout>
<!-- 第二个tab的布局 -->
<RelativeLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_centerInParent="true"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab2" />
</RelativeLayout>
<!-- 第三个tab的布局 -->
<RelativeLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_centerInParent="true"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab3" />
</RelativeLayout>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</TabWidget>
</LinearLayout>
</TabHost>
</LinearLayout>
2.Activity文件
public class TabActivity1 extends Activity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_1);
mTabHost = (TabHost) findViewById(R.id.tabhost1);
//1.初始化TabHost容器
mTabHost.setup();
//2.在TabHost创建标签,然后设置:标题/图标/标签页布局
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("标签1",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("标签2",null).setContent(R.id.tab2));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("标签3",null).setContent(R.id.tab3));
}
}
public class TabActivity1 extends Activity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_1);
mTabHost = (TabHost) findViewById(R.id.tabhost1);
//1.初始化TabHost容器
mTabHost.setup();
//2.在TabHost创建标签,然后设置:标题/图标/标签页布局
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("标签1",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("标签2",null).setContent(R.id.tab2));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("标签3",null).setContent(R.id.tab3));
}
}
方式二: tab项分开
1。
public class TabActivity2 extends Activity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_2);
mTabHost = (TabHost) findViewById(R.id.tabhost2);
//1.初始化TabHost容器
mTabHost.setup();
//把tab组件载入到tabHost父窗口中
LayoutInflater i=LayoutInflater.from(this);
i.inflate(R.layout.tab_1, mTabHost.getTabContentView());
i.inflate(R.layout.tab_2, mTabHost.getTabContentView());
i.inflate(R.layout.tab_3, mTabHost.getTabContentView());
//在TabHost创建标签,然后设置:标题/图标/标签页布局
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("标签1").setContent(R.id.tab1));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("标签2").setContent(R.id.tab2));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("标签3").setContent(R.id.tab3));
}
}
3. 使用TabActivity
public class TabActivity3 extends TabActivity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.activity_tab_3,
mTabHost.getTabContentView(), true);
//在TabHost创建标签,然后设置:标题/图标/标签页布局
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("标签1").setContent(R.id.tab1));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("标签2").setContent(R.id.tab2));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("标签3").setContent(R.id.tab3));
}
}
0 0
- Tabhost的简单使用
- tabHost的简单使用
- Tabhost的简单使用
- TabHost空间的简单使用
- tabHost选项卡的简单使用
- TabHost简单使用
- TabHost简单使用示例
- <TabHost> 简单使用
- Andriod TabHost的使用TabHost
- tabhost简单使用及tabhost源码分析
- tabhost简单使用及tabhost源码分析
- 简单的TabHost
- Tabhost的简单运用
- Tabhost的简单用法
- TabHost的简单实用
- Android---61---TabHost简单使用
- Android TabHost的使用
- Android TabHost的使用
- 深入usb网络共享(一) usb共享的开启流程(and5.1)
- Linux应用程序应该存放在什么位置
- 生成唯一订单
- 零基础学iOS开发
- quick中触摸事件响应机制
- tabHost的简单使用
- CRC8查表法
- Uva 307 Sticks(IDA*)
- python Mysql 数据库导入文本数据,中文乱码
- javascript json对象与json格式字符串区别
- Java System.arraycopy()方法
- Java 文件或者文件夹的删除
- 面试题初级-求字符串最后一个单词的长度和判断ip地址的合法性
- 儿童识字量测试3090字(倪铂颖)