用TabHost 来实现顶部选项卡,上代码:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <TabHost
android:id="@+id/tabMenu"
android:layout_width="match_parent"
android:layout_height="0dp"> <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="match_parent"> <LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> </LinearLayout> <LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> </LinearLayout> <LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> </LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost> </android.support.constraint.ConstraintLayout>

主方法MainActivity.java

package action.sun.com.tabhost;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost; public class MainActivity extends AppCompatActivity { private TabHost tabhost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //得到TabHost对象实例
tabhost =(TabHost) findViewById(R.id.tabMenu); //调用 TabHost.setup()
tabhost.setup();
//创建Tab标签
tabhost.addTab(tabhost.newTabSpec("one").setIndicator("红色").setContent(R.id.tab1));
tabhost.addTab(tabhost.newTabSpec("two").setIndicator("黄色").setContent(R.id.tab2));
tabhost.addTab(tabhost.newTabSpec("three").setIndicator("黄色").setContent(R.id.tab3)); tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String s) {
Log.d("xxx", "onTabChanged: ="+s);
if (s.equals("one")){
//可是让viewpage的视图显示出来
//viewPager.setCurrentItem(0);
}else if (s.equals("two")){
////可是让viewpage的视图显示出来
// viewPager.setCurrentItem(1);
}
}
});
}
}

代码很简单,实现效果:

Android TabHost的使用 顶部选项卡的更多相关文章

  1. Android应用底部导航栏(选项卡)实例

    现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其 ...

  2. Android TabHost使用

    TabHost是Android中自带的选项卡控件,效果图如下: 主布局文件 <RelativeLayout xmlns:android="http://schemas.android. ...

  3. TabTopLayout【自定义顶部选项卡区域(固定宽度且居中)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 自定义顶部选项卡并居中显示.结合显示/隐藏view的方式实现切换功能(正常情况下可能是切换fragment). 效果图 代码分析 T ...

  4. TabTopUnderLineLayout【自定义顶部选项卡(带下划线)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 自定义顶部选项卡布局LinearLayout类,实现带下划线样式的效果. 备注:如果配合Fragment的话,MainActivit ...

  5. TabTopAutoLayout【自定义顶部选项卡区域(带下划线)(动态选项卡数据且可滑动)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 自定义顶部选项卡布局LinearLayout类,实现带下划线且可滑动效果.[实际情况中建议使用RecyclerView] 备注:如果 ...

  6. Android开发学习之TabView选项卡具体解释 -- 基于Android4.4

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/he90227/article/details/24474197 直接上代码 -- 基于Android ...

  7. android去掉滑动到顶部和底部的阴影

    android去掉滑动到顶部和底部的阴影 <ListView android:id="@+id/listView" android:layout_width="ma ...

  8. Android tabhost下的activity怎样获取传来的值

    android tabhost下的activity怎样获取传来的值,具体解决方案如下: 解决方案: 其他activity设置intent:Intent intent=new Intent(); int ...

  9. Android - TabHost 与 Fragment 制作页面切换效果

    Android - TabHost 与 Fragment 制作页面切换效果 Android API 19 , API 23 三个标签页置于顶端 效果图: 在文件BoardTabHost.java中定义 ...

随机推荐

  1. Linux系统排查——CPU负载篇

    本随笔介绍CPU负载的排查手段. 查看系统负载的工具:uptime,w,都能查看系统负载,系统平均负载是处于运行或不可打扰状态的进程的平均数, 可运行:运行态,占用CPU,或就绪态,等待CPU调度. ...

  2. HANA初印象

    今天手懒,看了一些SAP HANA 的一些外文介绍,不翻译了,直接剽窃过来,供参考. 1. HANA 是什么东西? “HANA doesn't actually mean anything, but ...

  3. python opencv 按一定间隔截取视频帧

    前言关于opencvOpenCV 是 Intel 开源计算机视觉库 (Computer Version) .它由一系列 C 函数和少量 C++ 类构成,实现了图像处理和计算机视觉方面的很多通用算法. ...

  4. Oracle 11g安装,新建表空间和用户

    windows版: 官网下载,两个压缩包,总共2G,解压到同一个文件夹下,setup.exe双击执行,首次安装的话,一直下一步就可以了,最后一步可能时间比较长,等着就好了,一般性能的机器大概要半小时左 ...

  5. 修改eclipse.ini文件指定jdk

    在eclipse.ini文件顶部插入 -vmE:/kfgj/Java/jdk1.7.0_21/bin/javaw.exe

  6. win7重命名文件时 提示 “指定的设备名无效”的解决办法

    同事从mac上传一个文件夹到win7上,但是少了一张图片con.jpg.查了半天发现将备份文件改名为con.jpg时提示 “指定的设备名无效”. 谷歌了下,发现了问题所在.坑爹的win7. 从不同的系 ...

  7. Outlook 如何初始化邮箱

    首先我们找到邮箱的安装位置,我们可以右键Outlook,然后看其属性.找到其安装位置,复制下来,比如说 "C:\Program Files\Microsoft Office\root\Off ...

  8. php分享二十八:mysql运行中的问题排查

    一:杀掉mysql连接的方法: kill thread_id:  杀掉当前进程,断开连接 kill query thread_id: 只杀掉某连接当前的SQL,而不断开连接. 批量杀死MySQL连接的 ...

  9. 查询系统负载信息 Linux 命令详解

    linux uptime命令主要用于获取主机运行时间和查询linux系统负载等信息.uptime命令过去只显示系统运行多久.现在,可以显示系统已经运行了多长 时间,信息显示依次为:现在时间.系统已经运 ...

  10. vim的Tab键

    vim中默认的tab键大约是6个空格(目测)的宽度.如果想修改为4个空格,用以下命令:    shiftwidth=4    softtabstop-4shiftwidth的含义是:回车后需要缩进时, ...