Android TabHost的使用
标签显示界面的主要特点是可以在一个窗口中显示多组标签栏的类容。
在Android系统中,每个标签栏称为一个Tab,而包含多个标签栏的内容就称为TabHost。
通过TabHost的继承结构来看,TabHost类是FrameLayout的子类。
实现标签显示界面有两种方式可供选择。
- 直接让一个Activity继承TabActivity类。
- 利用findViewById()方法取得TabHost组件,并进行一些配置。
下面我们用两个简单例子来体验一下:
第一种继承TabActivity类:(注意:TabActivity类已被官方提出抛弃)
首先就是布局文件tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_edit"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入检索关键字..."
android:textSize="18px" />
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_clock"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<AnalogClock
android:id="@+id/myAnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_sex"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/male"
android:checked="true"
android:text="性别:男" />
<RadioButton
android:id="@+id/female"
android:text="性别:女" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
其次就是默认的MainActivity的实现:
public class MyTabHostDemo extends TabActivity { // 继承了TabActivity
private TabHost myTabHost;
private int[] layRes = new int[] { R.id.tab_edit, R.id.tab_clock,
R.id.tab_sex }; // 这些是内嵌布局文件的ID @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.myTabHost = super.getTabHost(); // 取得TabHost对象
LayoutInflater.from(this).inflate(R.layout.tab,
this.myTabHost.getTabContentView(), true); // true表示实例化布局文件中的组件
for (int x = 0; x < this.layRes.length; x++) {
TabSpec myTab = this.myTabHost.newTabSpec("tab" + x) ;
myTab.setIndicator("标签 - " + x) ;
myTab.setContent(this.layRes[x]) ;
this.myTabHost.addTab(myTab) ;
}
}
}
到此,一个简单的TabHost的使用就完成了。
第二种方式的实现:
首先,我们还是是布局文件的实现。
tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TabHost01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" >
</TextView>
</LinearLayout> <LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <TextView
android:id="@+id/TextView02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="two" >
</TextView>
</LinearLayout> <LinearLayout
android:id="@+id/LinearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <TextView
android:id="@+id/TextView03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="three" >
</TextView>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</LinearLayout> </TabHost>
main.xml:(这个不是默认.JAVA文件加载的布局文件,只是为了方便没有新建布局文件。)
<?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" >
<TextView
android:id="@+id/TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nihao!!!"/> </LinearLayout>
实现默认的MainActivity:
public class MainActivity extends ActivityGroup {//ActivityGroup类也已被建议遗弃
private TabHost tabHost;
Intent oneIntent; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
oneIntent = new Intent();
oneIntent.setClass(this, testActivity.class);
tabHost = (TabHost) this.findViewById(R.id.TabHost01);//获得对象
tabHost.setup();//建立对象(与上一行代码必须同时存在)
tabHost.setup(this.getLocalActivityManager());//要实现Intent跳转,必须要这句话。
try { tabHost.addTab(tabHost
.newTabSpec("tab_1")
.setContent(R.id.LinearLayout1)
.setIndicator("TAB1",
this.getResources().getDrawable(R.drawable.img1)));
tabHost.addTab(tabHost
.newTabSpec("tab_2")
.setContent(R.id.LinearLayout2)
.setIndicator("TAB2",
this.getResources().getDrawable(R.drawable.img2)));
tabHost.addTab(tabHost.newTabSpec("tab_3")
.setContent(R.id.LinearLayout3).setIndicator("TAB3"));
tabHost.addTab(tabHost
.newTabSpec("tab_4")
.setIndicator("TAB4",
this.getResources().getDrawable(R.drawable.img3))
.setContent(oneIntent));
tabHost.setCurrentTab(1); // 设置默认的页面
} catch (Exception ex) {
ex.printStackTrace();
Log.d("EXCEPTION", ex.getMessage());
}
} }
新建一个用于Intent跳转的Activity:
public class testActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView) findViewById(R.id.TV);
tv.setText("已经跳转了");
} }
接下来运行即可。
这样,用两个例子分别实现了两种不同的实现方式。
Android TabHost的使用的更多相关文章
- Android tabhost下的activity怎样获取传来的值
android tabhost下的activity怎样获取传来的值,具体解决方案如下: 解决方案: 其他activity设置intent:Intent intent=new Intent(); int ...
- Android - TabHost 与 Fragment 制作页面切换效果
Android - TabHost 与 Fragment 制作页面切换效果 Android API 19 , API 23 三个标签页置于顶端 效果图: 在文件BoardTabHost.java中定义 ...
- Android——TabHost(标签容器)相关知识总结贴
android 2.3 r1 中文 api (58) —— TabHost http://www.apkbus.com/android-18911-1-1.html android中文api (5 ...
- Android TabHost使用
TabHost是Android中自带的选项卡控件,效果图如下: 主布局文件 <RelativeLayout xmlns:android="http://schemas.android. ...
- Android -- TabHost
TabHost 也就相当于Windows下的选项框 有两种实现方式 1. 继承TabActivity (已经废弃):从TabActivity中用getTabHost()方法获取TabHost 2. ...
- Android -- TabHost、Fragment、状态保存、通信
工程结构 TabAFm到Ta ...
- Android TabHost中Activity之间传递数据
例子1: TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost); tabhost.setup(this.getLocalActi ...
- Android TabHost 动态修改图标或者动态改变标题
那时客户需要实现在TabHost标题上动态显示从数据库获取的个数.起初这样思考的,从数据库 获取个数是非常简单,但是要把获取的个数显示在TabHost标题,思前想后,想用Handler来异步实现消息传 ...
- Android TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题
最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. ...
- Android TabHost TabWidget 去除黑线(底部下划线)
采用TabHost布局时,往往会发现默认的系统风格与软件风格很不协调,比如TabWidget的下划线影响布局效果.通常情况下会去除其下划线.如果是采用xml布局文件,在TabWidget的属性项设置a ...
随机推荐
- 在DDMS中查看网络使用详情
在Android 4.0设置中的“流量使用情况”允许长期统计每个App如何使用网络资源.从4.0.3开始,配合最新发布的DDMS r17(在ADT r17 插件中有集成),您可以实时的在DDMS中查看 ...
- BZOJ2818: Gcd 欧拉函数求前缀和
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 如果两个数的x,y最大公约数是z,那么x/z,y/z一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...
- POJ 1847 Tram dij
分析:d[i]表示到i点,最少的操作数 #include<cstdio> #include<cstring> #include<queue> #include< ...
- selenium1.0和selenium2.0页面等待处理详解
一.selenium1.0页面等待 1.……AndWait 经常会看到, selenium action命令中很多有这种……AndWait后缀, 例如click和clickAndWait命令: cli ...
- 【转】Django+Mysql安装配置详解(Linux)
参考:http://dmyz.org/archives/110 报错TemplateDoesNotExist at 解决: 新建mysite/articles/article.html文件: 文件内容 ...
- qosort 使用使用小例子
输入 1500 3150 300100 200 输出结果470 471100 200150 300470 471 #include <iostream> #inc ...
- HTML5/CSS3动画应用
http://www.html5tricks.com/cool-html5-css3-animation.html
- 安装Ambari
1.yum install pdsh 这玩意一般系统都没带 2.检查下umask码,022是需要的 3.获取ambari的官方repo文件,并安装repo文件 wget http://publi ...
- HW4.40
public class Solution { public static void main(String[] args) { long positiveSide = 0; long negativ ...
- Dynamices CRM Permission Issue (Security role UI to privilege mapping)'s solution
select * from privilege where privilegeid = 'a4736385-9763-4a64-a44b-cd5933edc631' Security role UI ...