zoukankan      html  css  js  c++  java
  • Android中动态添加tab

    来源过于啰嗦,这里只有简化后的。

      转载请注明出处  http://www.cnblogs.com/zaiyuzhong/p/add-tab-dynamic-in-android.html

    建立对应的布局配置:/res/layout/activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <TabHost    android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/tabHost"
                xmlns:android="http://schemas.android.com/apk/res/android">
     
        <TabWidget
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs"/>
     
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@android:id/tabcontent">
     
        </FrameLayout>
     
    </TabHost>
     1 import android.view.Menu;
     2 import android.view.View;
     3  
     4 import android.widget.AnalogClock;
     5  
     6 import android.widget.TabHost;
     7 import android.widget.TabHost.TabSpec;
     8 import android.widget.TextView;
     9  
    10  
    11 public class MainActivity extends FragmentActivity {
    12  
    13     @Override
    14     protected void onCreate(Bundle savedInstanceState) {
    15         super.onCreate(savedInstanceState);
    16         setContentView(R.layout.activity_main);
    17          
    18         TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
    19         tabHost.setup();
    20  
    21         TabSpec spec1=tabHost.newTabSpec("Tab1");
    22         spec1.setContent(new TabHost.TabContentFactory() {
    23             public View createTabContent(String tag) {
    24                 TextView txtView = new TextView(MainActivity.this);
    25                 txtView.setText("Tab Text in createTabContent");
    26                 return txtView;
    27             }
    28         });
    29         spec1.setIndicator("Tab Text for setIndicator");
    30          
    31  
    32         TabSpec spec2=tabHost.newTabSpec("Tab2");
    33         spec2.setIndicator("Tab Clock");
    34         spec2.setContent(new TabHost.TabContentFactory() {
    35             public View createTabContent(String tag) {
    36                 return(new AnalogClock(MainActivity.this));
    37             }
    38         });
    39         spec2.setIndicator("Clock");
    40          
    41         tabHost.addTab(spec1);
    42         tabHost.addTab(spec2);
    43     }
    代码
  • 相关阅读:
    (转载)林轩田机器学习基石课程学习笔记1 — The Learning Problem
    二、HDFS(架构、读写、NN)
    剑指:和为S的两个数字
    剑指:和为S的连续正数序列
    Hive:数据倾斜
    linux如何查看端口被哪个进程占用
    du查看某个文件或目录占用磁盘空间的大小
    剑指:滑动窗口的最大值
    leetcode之求众数
    剑指:重建二叉树
  • 原文地址:https://www.cnblogs.com/zaiyuzhong/p/add-tab-dynamic-in-android.html
Copyright © 2011-2022 走看看