Android TabHost如何实现顶部选项卡

 更新时间:2020年09月23日 15:38:24   作者:手撕高达的村长  
这篇文章主要介绍了Android TabHost如何实现顶部选项卡,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

用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);
        }
      }
    });
  }
}

实现效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android5.0+ CollapsingToolbarLayout使用详解

    Android5.0+ CollapsingToolbarLayout使用详解

    这篇文章主要为大家详细介绍了Android5.0+ CollapsingToolbarLayout使用,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • android连续拖动导致挂起的解决方法

    android连续拖动导致挂起的解决方法

    本文给大家分享的是在安卓的项目开发中遇到连续拖动对象,导致挂起的问题的解决方法,也是经过很多网友的提示,最终才找到解决方法,这里记录一下,分享给大家。
    2015-05-05
  • Android自定View实现滑动验证效果的代码

    Android自定View实现滑动验证效果的代码

    这篇文章主要介绍了Android自定View实现滑动验证效果,代码分为自定义属性代码和自定义view代码及使用方法,本文给大家介绍的非常详细,需要的朋友可以参考下
    2021-12-12
  • android 开发 文件读写应用案例分析

    android 开发 文件读写应用案例分析

    在Android应用中保存文件会使用到文件读写技术,本文将详细介绍,需要的朋友可以参考下
    2012-12-12
  • Android App界面的ListView布局实战演练

    Android App界面的ListView布局实战演练

    这篇文章主要介绍了Android App界面的ListView布局方法,文中分了三种情况通过实例来讲解,ListView适用于功能最简单的应用程序UI布局,需要的朋友可以参考下
    2016-04-04
  • Android 读写文件方法汇总

    Android 读写文件方法汇总

    以下是对Android中读写文件的方法进行了汇总介绍,需要的朋友可以过来参考下
    2013-07-07
  • Android自动播放Banner图片轮播效果

    Android自动播放Banner图片轮播效果

    这篇文章主要介绍了Android自动播放Banner图片轮播效果的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android获取设备IP地址的示例代码

    Android获取设备IP地址的示例代码

    ​我们在做Android平台GB28181设备接入模块的时候,上层需要拿到设备的IP地址,所以这篇文章主要为大家介绍了Android获取设备IP地址的相关资料,需要的可以参考下
    2023-11-11
  • Android通过bin二进制程序调用jar原理

    Android通过bin二进制程序调用jar原理

    最近在研究monkey测试,发现monkey测试的代码都是JAVA编写的,通过编译生成jar包,而我们在执行测试时直接执行/system/bin/monkey这个二进制程序的,那么它是如何能调起java程序的呢,本文小编给大家介绍了Android通过bin二进制程序调用jar原理,需要的朋友可以参考下
    2023-10-10
  • Android 自定义Dialog去除title导航栏的解决方法

    Android 自定义Dialog去除title导航栏的解决方法

    今天小编就为大家分享一篇Android 自定义Dialog去除title导航栏的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07

最新评论