android选项卡TabHost功能和用法详解(1)

本文详细介绍了如何在Android的TabActivity中使用TabHost组件,展示了如何创建多个Tab页面并设置其内容,以及如何监听标签切换事件。同时提到了一份全面的Android开发学习资料,旨在帮助开发者系统学习和提升技能。
摘要由CSDN通过智能技术生成

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“button”/>

<LinearLayout

android:id=“@+id/tab2”

android:orientation=“vertical”

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<EditText

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“edittext”/>

<LinearLayout

android:id=“@+id/tab3”

android:orientation=“vertical”

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<TextView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“textview”/>

<LinearLayout

android:id=“@+id/tab4”

android:orientation=“vertical”

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<EditText

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“edittext”/>

<TextView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“textview”/>

MainActivity.java(大部分都有注释)

package com.example.tab;

import android.app.Activity;

import android.app.TabActivity;

import android.content.res.Resources;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.TabHost;

import android.widget.TabHost.OnTabChangeListener;

import android.widget.Toast;

public class MainActivity extends Activity {

private TabHost tabs;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//获取该Activity里面的TabHost组件

tabs = (TabHost) findViewById(android.R.id.tabhost);

//初始化TabHost容器

tabs.setup();

//资源对象

Resources res = getResources();

TabHost.TabSpec spec;

//创建第一个Tab页

spec = tabs.newTabSpec(“tab1”)

.setIndicator(“tab1”) //设置标题

.setContent(R.id.tab1); //设置内容

//添加第一个标签页

tabs.addTab(spec);

//res.getDrawable(R.drawable.wo)添加图片

spec = tabs.newTabSpec(“tab2”).setIndicator(“tab2”, res.getDrawable(R.drawable.wo)).setContent(R.id.tab2);

tabs.addTab(spec);

spec = tabs.newTabSpec(“tab3”).setIndicator(“tab3”).setContent(R.id.tab3);

tabs.addTab(spec);

spec = tabs.newTabSpec(“tab4”).setIndicator(“tab4”).setContent(R.id.tab4);

tabs.addTab(spec);

//设置默认选项卡,第几个,从0开始

tabs.setCurrentTab(1);

//标签切换事件处理,setOnTabChangedListener

tabs.setOnTabChangedListener(new OnTabChangeListener(){

//tabId是newTabSpec参数设置的tab页名,

@Override

public void onTabChanged(String tabId) {

if (tabId.equals(“tab1”)) { //第一个标签

Toast.makeText(MainActivity.this, “点击tab1”, Toast.LENGTH_SHORT).show();

}

if (tabId.equals(“tab2”)) { //第二个标签

Toast.makeText(MainActivity.this, “点击tab2”, Toast.LENGTH_SHORT).show();

}

if (tabId.equals(“tab3”)) { //第三个标签

Toast.makeText(MainActivity.this, “点击tab3”, Toast.LENGTH_SHORT).show();

}

if (tabId.equals(“tab4”)) { //第四个标签

Toast.makeText(MainActivity.this, “点击tab4”, Toast.LENGTH_SHORT).show();

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

欢迎大家一起交流讨论啊~

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

4/03/13/H4lCoPEF.jpg" />

最后

如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

[外链图片转存中…(img-PzI6M2Yp-1713284506082)]

欢迎大家一起交流讨论啊~

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 13
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TabHostAndroid 中的一个容器,用于实现多个标签页之间的切换。下面是 TabHost 的使用方法详解: 1. 在 XML 布局文件中添加 TabHost 控件: ```xml <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> </TabHost> ``` 2. 在 Activity 中初始化 TabHost: ```java TabHost tabHost = findViewById(android.R.id.tabhost); tabHost.setup(); ``` 3. 添加标签页: ```java TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("Tab1"); tabSpec1.setIndicator("Tab 1"); tabSpec1.setContent(R.id.tab1_content); TabHost.TabSpec tabSpec2 = tabHost.newTabSpec("Tab2"); tabSpec2.setIndicator("Tab 2"); tabSpec2.setContent(R.id.tab2_content); tabHost.addTab(tabSpec1); tabHost.addTab(tabSpec2); ``` 4. 创建标签页的布局: 在布局文件中创建用于显示每个标签页内容的 View,例如: ```xml <LinearLayout android:id="@+id/tab1_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 1 的内容 --> </LinearLayout> <LinearLayout android:id="@+id/tab2_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 2 的内容 --> </LinearLayout> ``` 以上就是 TabHost 的基本使用方法。通过添加不同的标签页和对应的布局,可以实现多个标签页之间的切换和显示不同的内容。你可以根据自己的需求来定制标签页的样式和内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值