android TabHost

<?xml version="1.0" encoding="utf-8"?>
<!--TabHost布局文件-->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabhost"
    >
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
        <!--TabWidget中android:id="@android:id/tabs"引用的是系统中的id,这里只有是系统中的id调用setup()方法时系统才能找到对应的组件-->
        <TabWidget
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/tabs"
        />

         <!--FrameLayout中@android:id/tabcontent引用的是系统中的id,这里只有是系统中的id调用setup()方法时系统才能找到对应的组件-->
        <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/tabcontent"
        >
                <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/page1"
                >
                   <TextView
                   android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="这是第一个标签页"
                   />
                </LinearLayout>
        
               <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/page2"
                >
                   <TextView
                   android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="这是第二个标签页"
                   />
                </LinearLayout>
            
                <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/page3"
                >
                   <TextView
                   android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="这是第三个标签页"
                   />
                </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>
public class MainActivity extends Activity {
    TabHost tabHost;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Debug.startMethodTracing("itcast");

        //根据id找到TabHost组件
        tabHost = (TabHost) this.findViewById(R.id.tabhost);
        //调用此方法,系统会根据布局文件(系统中的id)找到TabWidget和FrameLayout组件
        tabHost.setup();
        
        TabSpec tabSpec = tabHost.newTabSpec("page1");
        //tabSpec.setIndicator("首页", getResources().getDrawable(R.drawable.i1));
        tabSpec.setIndicator(createTabView("首页"));//设置自定义的标签
        tabSpec.setContent(R.id.page1);//设置标签对应的页面
        tabHost.addTab(tabSpec);//添加标签页到TabHost中
        
        tabSpec = tabHost.newTabSpec("page2");
       // tabSpec.setIndicator("第二页", getResources().getDrawable(R.drawable.i2));
        tabSpec.setIndicator(createTabView("第二页"));
        tabSpec.setContent(R.id.page2);
        tabHost.addTab(tabSpec);
        
        tabSpec = tabHost.newTabSpec("page3");
        //tabSpec.setIndicator("第三页", getResources().getDrawable(R.drawable.i7));
        tabSpec.setIndicator(createTabView("第三页"));
        tabSpec.setContent(R.id.page3);
        tabHost.addTab(tabSpec);
        
        tabHost.setCurrentTab(0);
    }
    
    @Override
    protected void onDestroy() {
        Debug.stopMethodTracing();
        super.onDestroy();
    }

    private View createTabView(String name) {
        //View tabView = getLayoutInflater().inflate(R.layout.tab, null);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setBackgroundColor(0xFFFFFF);
        
        TextView textView = new TextView(this);
        textView.setText(name);
        textView.setBackgroundResource(R.drawable.tab_bg);
        textView.setTextColor(0xFFFFFF);
        textView.setTextSize(18.0f);
        textView.setGravity(Gravity.CENTER);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        linearLayout.addView(textView, params);
        
        return linearLayout;
    }
}
原文地址:https://www.cnblogs.com/heml/p/3522070.html