TabHost的使用
来源:互联网 发布:mac 终端设置时间 编辑:程序博客网 时间:2024/06/05 07:28
TabActivity是一个过时的类,推荐使用Fragment.
实现选项卡的一般步骤:
1.在布局文件中添加实现选项卡所需要的TabHost,TabWidget,和FrameLayout组件。
2.编写各标签页中要显示内容所对应的xml布局文件。
3.在activity中,获取并初始化TabHost组件。
4.为TabHost对象添加标签页
xml文件:
<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/c92_tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" > <AnalogClock android:id="@+id/c92_tab1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerHorizontal="true" /> <RelativeLayout android:id="@+id/rll" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/c92_tab2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="A semi-random Button" /> <Button android:id="@+id/c92_tab3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/c92_tab2" android:text="A semi-random Button" /> </RelativeLayout> </FrameLayout> </LinearLayout></TabHost>在xml文件中添加选项卡组件时,必须使用系统的id,否则会报错。
activity中:
public class MainActivity extends Activity {private TabHost tabs;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tabhost);// 步骤1:获得TabHost的对象,并进行初始化setup()tabs = (TabHost) findViewById(R.id.c92_tabhost);tabs.setup();// 步骤2:通过TabHost.TabSpec增加tab的一页,通过setContent()增加内容,通过setIndicator增加页的标签/* (1)增加第1页 */TabHost.TabSpec spec; spec = tabs.newTabSpec("Tag1");spec.setContent(R.id.c92_tab1);spec.setIndicator("Clock");tabs.addTab(spec);/* (2)增加第2页 */spec = tabs.newTabSpec("Tag2");spec.setContent(R.id.rll);spec.setIndicator("Button");tabs.addTab(spec);// 步骤3:可通过setCurrentTab(index)指定显示的页,从0开始计算。tabs.setCurrentTab(1);}//动态添加一个Tab页public void onClick(View view){TabHost.TabSpec spec = tabs.newTabSpec("tag3"); spec.setContent(new TabHost.TabContentFactory() { /*createTabContent将返回View,这里我们简单用一个模拟时钟*/ public View createTabContent(String tag) { return new AnalogClock(MainActivity.this); } }); spec.setIndicator("other"); tabs.addTab(spec);}}如上button监听中,可以动态地添加一个tab标签
0 0
- Andriod TabHost的使用TabHost
- Android TabHost的使用
- Android TabHost的使用
- TabActivity & TabHost 的使用
- Android TabHost的使用
- Tabhost的使用
- Android TabHost的使用
- android Tabhost的使用
- TabHost的使用
- Android TabHost的使用 .
- Android TabHost的使用
- Android TabHost的使用
- TabHost的使用
- TabHost的使用
- TabHost的使用
- Tabhost的简单使用
- TabHost的使用
- TabHost的使用
- 一个中兴的面试题,输入两个数n和m,从数列1,2,3……n中随意取几个数,使其和等于m,要求将其中所有组合列出来编程求解(c语言递归函数分解法)
- 用QRCode生成带有中间logo图的二维码
- android自定义view-绘制顺序及相关原理
- Spring3.0和3.1新定时任务节点
- [转]linux进程的最大文件数
- TabHost的使用
- jpa 由数据库反向生成实体
- C++中析构函数需要定义成虚函数的原因
- 圣诞节“马上有对象” 男士求爱成功率高达55%
- weak alias
- 从QQ网站中提取的纯JS省市区三级联动
- 黑马程序员_HTML中CSS使用的几种方法
- Oracle导出存储过程
- 分享SEO博客推广的五年夜体点