Android入门之TabHost与TabWidget实例解析
作者:shichen2014 时间:2022-12-07 23:03:42
本文实例介绍的是Android的Tab控件,Tab控件可以达到分页的效果,让一个屏幕的内容尽量丰富,当然也会增加开发的复杂程度,在有必要的时候再使用。Android的Tab控件使用起来有点奇怪,必须包含和按照以下的顺序:
TabHost控件->TabWidget(必须命名为tabs)->FrameLayout(必须命名为tabcontent)。
先来贴出本例运行的截图:
main.xml的源码如下:
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/TabHost1">
<TabWidget android:id="@android:id/tabs"
android:layout_height="wrap_content" android:layout_width="fill_parent">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:paddingTop="65px" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab1" android:orientation="vertical" android:layout_width="fill_parent">
<EditText android:layout_height="wrap_content" android:id="@+id/edtTab1" android:layout_width="fill_parent"></EditText>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab1" android:text="Tab1"></Button>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab2" android:layout_width="fill_parent" android:orientation="horizontal">
<EditText android:layout_height="wrap_content" android:id="@+id/edtTab2" android:layout_width="wrap_content" android:layout_weight="300"></EditText>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab2" android:text="Tab2"></Button></LinearLayout>
</FrameLayout>
</TabHost>
java程序源码如下:
package com.testTab;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class testTab extends TabActivity {//基于TabActivity构建
Button btnTab1,btnTab2;
EditText edtTab1,edtTab2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabs = getTabHost();
//设置Tab1
TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setIndicator("tab1"); // 设置tab1的名称
tab1.setContent(R.id.Tab1); // 关联控件
tabs.addTab(tab1); // 添加tab1
btnTab1=(Button)this.findViewById(R.id.btnTab1);
edtTab1=(EditText)this.findViewById(R.id.edtTab1);
btnTab1.setOnClickListener(new ClickEvent());
//设置Tab2
TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setIndicator("tab2");
tab2.setContent(R.id.Tab2);
tabs.addTab(tab2);
btnTab2=(Button)this.findViewById(R.id.btnTab2);
edtTab2=(EditText)this.findViewById(R.id.edtTab2);
btnTab2.setOnClickListener(new ClickEvent());
tabs.setCurrentTab(0);
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
if(v==btnTab1)
{
edtTab1.setText("tab1");
}
else if(v==btnTab2)
{
edtTab2.setText("tab2");
}
}
}
}
标签:Android,TabHost,TabWidget
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java生成图片验证码实例代码
2022-01-06 04:01:37
使用C++程序获取新浪行情数据的方法
2022-06-16 13:14:35
![](https://img.aspxhome.com/file/2023/5/117755_0s.png)
解决Android WebView拦截url,视频播放加载失败的问题
2023-10-24 02:22:25
java中的Io(input与output)操作总结(四)
2021-10-11 03:14:19
![](https://img.aspxhome.com/file/2023/0/97950_0s.png)
android 如何获取MCC/MNC控制小区广播的开启
2023-02-02 21:00:00
使用Android Studio检测内存泄露(LeakCanary)
2022-08-31 11:08:12
![](https://img.aspxhome.com/file/2023/2/97872_0s.png)
Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】
2023-03-17 11:09:11
![](https://img.aspxhome.com/file/2023/5/80855_0s.jpg)
java多线程-读写锁原理
2021-07-20 17:28:52
SpringCloud之分布式配置中心Spring Cloud Config高可用配置实例代码
2021-06-23 16:48:08
![](https://img.aspxhome.com/file/2023/8/85408_0s.png)
Android视频悬浮窗口实现的示例代码
2022-08-01 06:50:33
![](https://img.aspxhome.com/file/2023/6/138386_0s.gif)
Java经典面试题最全汇总208道(三)
2023-11-15 23:30:42
![](https://img.aspxhome.com/file/2023/6/59066_0s.jpg)
java虚拟机中多线程总结
2022-09-01 22:55:48
![](https://img.aspxhome.com/file/2023/6/79956_0s.png)
Android中NestedScrolling滑动机制详解
2022-09-16 22:32:13
![](https://img.aspxhome.com/file/2023/9/106059_0s.png)
一个简单的toolabar结合drawlayout使用方法
2023-05-12 15:01:16
IntelliJ IDEA使用git初始化仓库的使用方法
2022-05-24 12:37:26
![](https://img.aspxhome.com/file/2023/4/68534_0s.png)
Android 模仿QQ侧滑删除ListView功能示例
2023-10-27 21:03:43
java图片验证码生成教程详解
2021-11-04 13:22:14
![](https://img.aspxhome.com/file/2023/7/60987_0s.jpg)
基于request获取访问者真实IP代码示例
2023-02-15 02:45:13
Spring Boot 中密码加密的两种方法
2021-09-12 15:08:38
![](https://img.aspxhome.com/file/2023/5/78895_0s.png)
Java生产者消费者模式实例分析
2023-12-13 02:12:59
![](https://img.aspxhome.com/file/2023/5/92635_0s.jpg)