天天看点

tabhost,framelayout,tabwidget(一)

学习自android4.x手机/平板电脑程序设计入门,应用到精通一书

tabhost,framelayout,tabwidget(一)

注意一下几点:

1.<TabWidget>标签的id,固定是@android:id/tabs,不能改

2.<FrameLayout>标签的id固定是@android:id/tabcontent,也不能改

activity_main.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/tabHost"

    android:layout_width="fill_parent"

    android:layout_height="match_parent" >

    <TabWidget

        android:id="@android:id/tabs"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:gravity="center_horizontal" />

    <FrameLayout

        android:id="@android:id/tabcontent"

        android:layout_width="fill_parent"

        android:layout_height="match_parent" >

        <LinearLayout

            android:id="@+id/tab1"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:orientation="vertical" 

            android:paddingTop="70dp">

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="tab1111111111111111111111111" />

        </LinearLayout>

    <LinearLayout

        android:id="@+id/tab2"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical" 

        android:paddingTop="70dp">

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="tab222222222222222222" />

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="tab222222222222222222" />

    </LinearLayout>    

    </FrameLayout>

</TabHost>

MainActivity

package com.tab1;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.TabHost;

import android.widget.TextView;

import android.widget.TabHost.TabSpec;

import android.widget.TabWidget;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//初始化TabHost

TabHost tabHost = (TabHost) findViewById(R.id.tabHost);

tabHost.setup();

// 标签一

TabSpec spec1 = tabHost.newTabSpec("tab1");

spec1.setContent(R.id.tab1);

spec1.setIndicator(

// tab上显示的文字和tab的背景图片

"tab1",

getResources().getDrawable(

android.R.drawable.ic_lock_idle_alarm));

tabHost.addTab(spec1);

// 标签二

TabSpec spec2 = tabHost.newTabSpec("tab2");

spec2.setContent(R.id.tab2);

spec2.setIndicator("tab2",

getResources().getDrawable(android.R.drawable.ic_btn_speak_now));

tabHost.addTab(spec2);

// 指定程序启动第一个显示的tab page

tabHost.setCurrentTab(0);

// 修改tab page标题的字体大小

TabWidget tabWidget = (TabWidget) findViewById(android.R.id.tabs);

View tabView = tabWidget.getChildTabViewAt(0);

TextView tab = (TextView) tabView.findViewById(android.R.id.title);

tab.setTextSize(30);

}

}

按下tab1:

tabhost,framelayout,tabwidget(一)

按下tab2:

tabhost,framelayout,tabwidget(一)