Android TabHost如何实现顶部选项卡

作者:手撕高达的村长 时间:2023-04-13 01:08:14 

用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如何实现顶部选项卡

来源:https://www.cnblogs.com/sunxun/p/9243852.html

标签:Android,TabHost,顶部,选项卡
0
投稿

猜你喜欢

  • Java操作redis设置第二天凌晨过期的解决方案

    2022-11-15 11:40:10
  • Java Servlet请求重定向的方法

    2022-05-23 16:56:02
  • 详解Java对象结构与对象锁的升级

    2021-12-05 16:18:38
  • C#在复杂多线程环境下使用读写锁同步写入文件

    2021-06-16 03:45:53
  • Java多线程回调方法实例解析

    2023-11-04 01:40:01
  • Android 中的危险权限详细整理

    2023-12-03 09:07:25
  • Android Studio Menu选择菜单的建立方法

    2023-07-28 03:46:31
  • C#泛型语法详解

    2023-01-28 18:20:12
  • Java中获取泛型类型信息的方法

    2022-06-30 16:06:34
  • android的消息处理机制(图文+源码分析)—Looper/Handler/Message

    2023-01-04 08:04:15
  • IDEA自定义pom依赖的步骤详解

    2021-11-06 04:14:32
  • RxJava 触发流基本原理源码解析

    2023-06-24 06:02:57
  • C++实现简单酒店管理系统

    2021-07-31 13:52:40
  • 深入解析kafka 架构原理

    2023-11-18 13:40:14
  • Java HashMap三种循环遍历方式及其性能对比实例分析

    2022-03-22 18:36:45
  • 在WinForm中发送HTTP请求的实现方法

    2023-01-28 10:47:35
  • Spring框架应用的权限控制系统详解

    2023-11-11 14:17:11
  • 如何在springboot中实现页面的国际化

    2021-08-13 03:33:07
  • Java IO学习之缓冲输入流(BufferedInputStream)

    2021-10-08 22:30:23
  • Android实现界面内嵌多种卡片视图(ViewPager、RadioGroup)

    2023-03-06 15:48:31
  • asp之家 软件编程 m.aspxhome.com