tabhost,framelayout,tabwidget(一)

本文介绍了一个使用 Android 的 TabHost 控件创建多标签界面的应用实例。通过设置固定的 TabWidget 和 FrameLayout ID,演示了如何添加标签页并配置其内容与指示器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

注意一下几点:

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:

按下tab2:








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值