转自:http://marshal.easymorse.com/archives/1739

在android中,也有类似分页标签(tab)的布局和组件。可以实现类似的功能。

看另外一个分页标签内容:

实现功能,继承TabActivity不是必须的,可以直接继承Activity编写Activity即可,见:

package com.easymorse; import android.app.Activity;import android.os.Bundle;import android.widget.TabHost; public class TabDemoActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        this.setTitle(“演示标签分页”);        TabHost tabHost=(TabHost) this.findViewById(R.id.tabhost);        tabHost.setup();        //添加第一个tab页面        TabHost.TabSpec spec=tabHost.newTabSpec(“clockTab”);        spec.setContent(R.id.AnalogClock01);        spec.setIndicator(“模拟时钟”);        tabHost.addTab(spec);        //添加第二个tab页面        spec=tabHost.newTabSpec(“buttonTab”);        spec.setContent(R.id.DigitalClock01);        spec.setIndicator(“数字时钟”);        tabHost.addTab(spec);        //设置默认页面        tabHost.setCurrentTab(0);    }}

对应的布局文件layout/main.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”>     <TabHost android:id=”@+id/tabhost” android:layout_width=”wrap_content”        android:layout_height=”wrap_content”>        <TabWidget android:id=”@android:id/tabs”            android:layout_width=”fill_parent” android:layout_height=”wrap_content” />        <FrameLayout android:id=”@android:id/tabcontent”            android:paddingTop=”80px” android:layout_width=”fill_parent”            android:layout_height=”fill_parent”>            <AnalogClock android:id=”@+id/AnalogClock01″                android:layout_width=”wrap_content” android:layout_height=”wrap_content”></AnalogClock>            <DigitalClock android:id=”@+id/DigitalClock01″                android:layout_width=”fill_parent” android:layout_height=”fill_parent”                android:layout_centerHorizontal=”true”></DigitalClock>        </FrameLayout>    </TabHost></LinearLayout>

这里需要注意以下几点:

  • TabHost是分页标签的总的容器,必须指定android:id为@android:id/tabhost
  • TabWidget放在TabHost容器中,必须指定android:id属性为@android:id/tabs
  • FrameLayout是Tab内容的容器,其中的每个内容都会被实现为一个分页标签,布局上要做处理,比如paddingTop,定位标签内容

更多相关文章

  1. 安全新手入坑——HTML标签
  2. 解决win8下AndroidSDK秒退的问题
  3. [无线] AndroidManifest.xml配置文件详解
  4. 安装android sdk22.2.1 应对错误 Error : Missing platform-tool
  5. Android(安卓)Switch控件(在android2.2 api8及以上使用)
  6. 在xml文件的Preference标签中,用给标签加参数
  7. Android(安卓)Listview分组特效:滑动分组标题当前固定,并随内容滑
  8. undefined reference to 'AndroidBitmap_getInfo'
  9. EditText限制字符输入

随机推荐

  1. EditText保留两位小数并输入不超过最大值
  2. 2.4.4 进度条
  3. 【Android】使用Git控制Android程序的git
  4. 启动 flutter项目时报Could not find com
  5. Android工具包
  6. Error:Execution failed for task ':app:
  7. Android am/pm命令用法
  8. adb连接不上模拟器的问题
  9. Android自定义View之一:初探实例
  10. Android 后台保活设计2019,最新版本