Android入门之TabHost与TabWidget实例解析

本文实例介绍的是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"); } } } }

时间: 2024-07-31 08:16:40

Android入门之TabHost与TabWidget实例解析的相关文章

Android入门之TabHost与TabWidget实例解析_Android

本文实例介绍的是Android的Tab控件,Tab控件可以达到分页的效果,让一个屏幕的内容尽量丰富,当然也会增加开发的复杂程度,在有必要的时候再使用.Android的Tab控件使用起来有点奇怪,必须包含和按照以下的顺序: TabHost控件->TabWidget(必须命名为tabs)->FrameLayout(必须命名为tabcontent). 先来贴出本例运行的截图: main.xml的源码如下: <?xml version="1.0" encoding="

Android入门之Gallery+ImageSwitcher用法实例解析_Android

继上一篇介绍了如何使用Gallery控件之后,本文就来讲一下Gallery 与ImageSwitcher的结合使用.本文所述实例代码将实现一个简单的浏览图片的功能. 先贴出程序运行截图如下: 除了Gallery可以拖拉切换图片,我在ImageSwitcher控件加入了setOnTouchListener事件实现,使得ImageSwitcher也可以在拖拉中切换图片.本例子依然使用JAVA的反射机制来自动读取资源中的图片. main.xml的源码如下: <?xml version="1.0&

Android入门之Gallery+ImageSwitcher用法实例解析

继上一篇介绍了如何使用Gallery控件之后,本文就来讲一下Gallery 与ImageSwitcher的结合使用.本文所述实例代码将实现一个简单的浏览图片的功能. 先贴出程序运行截图如下: 除了Gallery可以拖拉切换图片,我在ImageSwitcher控件加入了setOnTouchListener事件实现,使得ImageSwitcher也可以在拖拉中切换图片.本例子依然使用JAVA的反射机制来自动读取资源中的图片. main.xml的源码如下: <?xml version="1.0&

Android评分控件RatingBar使用实例解析_Android

无论游戏,应用,网站,都少不了评分控件.在Android SDK 中提供了 RatingBar控件来实现相应的工作. <RatingBar/>标签有几个常用评分相关属性 android:numStars,指定评分五角星数. android:rating,指定当前分数 android:stepSize, 指定分数增量 <RatingBar/>还有3种 常用的style属性 默认style 就是ratingBarStyle style ratingBarStyleIndicator 不

Android评分控件RatingBar使用实例解析

无论游戏,应用,网站,都少不了评分控件.在Android SDK 中提供了 RatingBar控件来实现相应的工作. <RatingBar/>标签有几个常用评分相关属性 android:numStars,指定评分五角星数. android:rating,指定当前分数 android:stepSize, 指定分数增量 <RatingBar/>还有3种 常用的style属性 默认style 就是ratingBarStyle style ratingBarStyleIndicator 不

Android ActionBar制作时钟实例解析_Android

本文实例为大家分享了Android ActionBar制作时钟的具体代码,供大家参考,具体内容如下 1. MainActivity.java   package com.example.days19actionbar07custom; import com.example.days19actionbar07custom.R; import android.app.Activity; import android.os.Bundle; import android.view.Menu; impor

Android 蓝牙开发实例解析_Android

在使用手机时,蓝牙通信给我们带来很多方便.那么在Android手机中怎样进行蓝牙开发呢?本文以实例的方式讲解Android蓝牙开发的知识.        1.使用蓝牙的响应权限 XML/HTML代码 <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN&qu

Android程序开发之自定义设置TabHost,TabWidget样式_Android

先看效果: 京东商城底部菜单栏 新浪微博底部菜单栏 本次学习效果图: 第一,主布局文件(启动页main.xml,位于res/layout目录下)代码 <?xml version="." encoding="utf-"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_paren

Android入门之LinearLayout、AbsoluteLayout的用法实例讲解

本文实例介绍了Android中LinearLayout.AbsoluteLayout的用法,希望能对于初学Android的朋友起到一点帮助作用.具体内容如下: Android 的UI 布局都以Layout 作为容器,并且在上面按照规定排列控件,这方面跟JAVA 的Swing 和LWUIT 很像.控件跟Layout 有很多属性是一样的,可以在Properties 里面修改,跟.NET/Delphi 等RAD 类似,其中最常用的属性有以下这些: id="@+id/edtInput",ID