选项卡使用方法二(Android学习随笔十三)

1、不继承TabActivity

2、在布局件中定义TabHost

注意:TabWidgetid必须是@android:id/tabsFrameLayoutid必须是@android:id/tabcontent

java代码:

package yc.demo;import android.app.Activity;import android.os.Bundle;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class TabhostDemo1Activity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.demo1);        //获取到TabHost        TabHost tabHost = (TabHost)this.findViewById(R.id.tabs);        tabHost.setup();        //获取一个新的TabHost.TabSpec,并关联到当前tabHost        //参数:所需的选项卡标签        TabSpec pSpes = tabHost.newTabSpec("parent");        pSpes.setIndicator("父类", this.getResources().getDrawable(R.drawable.msg_icon));        pSpes.setContent(R.id.textView1);                TabSpec subSpec = tabHost.newTabSpec("sub");        subSpec.setIndicator("子类",this.getResources().getDrawable(R.drawable.at_icon));        subSpec.setContent(R.id.textView2);        //向tabHost中添加选项卡        tabHost.addTab(pSpes);        tabHost.addTab(subSpec);    }}

xml布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TabHost android:id="@+id/tabs"        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_height="fill_parent"            android:layout_width="fill_parent">                        <TextView android:id="@+id/textView1"                android:layout_height="fill_parent"                android:layout_width="fill_parent"                android:text="parent"/>                        <TextView android:id="@+id/textView2"                android:layout_height="fill_parent"                android:layout_width="fill_parent"                android:text="sub"/>                    </FrameLayout>        </LinearLayout>    </TabHost></LinearLayout>


提示:选项卡的显示顺序是根据你添加选项卡的顺序定的。

更多相关文章

  1. Android(安卓)ListView去掉顶部阴影
  2. Android(安卓)自动化测试框架Instrumentation
  3. 2010.10.31———Android(安卓)04
  4. Android(安卓)UI开发第六篇——仿QQ的滑动Tab
  5. android拨打电话
  6. Get the Android(安卓)SDK---获取Android(安卓)SDK
  7. 2010.10.31———Android(安卓)04
  8. android ksoap2 访问https javax.net.ssl.SSLHandshakeException
  9. android4.2上获取应用程序大小的变更点

随机推荐

  1. Android 记录一个好用的文件存储操作工具
  2. android 禁止系统软键盘,拦截键盘事件
  3. Android(安卓)tips3
  4. android webview 获取geolocation
  5. Android 自定义UI--电池
  6. 【Android】Android permission 访问权限
  7. Android(安卓)多点触摸
  8. Android设置重复文字水印背景
  9. 在启动activity之前,调用application的onc
  10. android系统时间获取方式