Android TabHost如何实现顶部选项卡
作者:手撕高达的村长 发布时间:2023-04-13 01:08:14
标签: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);
}
}
});
}
}
实现效果
来源:https://www.cnblogs.com/sunxun/p/9243852.html


猜你喜欢
- spring schedule 动态配置执行时间之前saas平台实现动态修改定时任务的时间,都是通过xx-job这样的框架来实现,这样我们可
- 环境JDK 1.8Spring Boot 2.3.0.RELEASEMaven 3.6.1H2 数据库用户名密码登录首先,我们用 Sprin
- 前言C# 11 中即将到来一个可以让重视性能的开发者狂喜的重量级特性,这个特性主要是围绕着一个重要底层性能设施 ref 和 struct 的
- 前言Android 8.0系统更新之后,app的更新将不再像之前的系统版本一样能够直接下载安装包之后直接安装(以前安装未知来源应用的时候一般
- c#里面封装了几乎所有我们可以想到的和我们没有想到的类,流是读取文件的一般手段,那么你真的会用它读取文件中的数据了么?真的能读完全么?通常我
- 项目中需要webview重定向,但是由于一个webveiw里面有许多加载操作,因此在调用webview。goback()方法时,往往达不到我
- 1.刷新maven项目2.清理idea缓存3.maven clean install4.重新bulid5.如果使用了lombok插件开启之后
- 开放端口安全组没开放端口是原罪!!!导致好多BUG费时费力。Hbase悄悄 * 的用了好多端口,比如被我抓到的42239,直接搜索报错药不对症
- SessionSession对象用于获取与数据库的物理连接。 Session对象是重量轻,设计了一个互动是需要与数据库每次被实例化。持久化对
- 在逆向一个Android程序时,如果只是盲目的分析需要阅读N多代码才能找到程序的关键点或Hook点,本文将分享一下如何快速的找到APP程序的
- 介绍和使用场景1)什么是消息一个事件,需要广播或者单独传递给某个接口2)为什么使用这个配置更新了,但是其他系统不知道是否更新SpringCl
- 一、简介Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新spring应用的初始搭建以及开发过程。该框架使用
- 目录前言令牌中继令牌难道不能在Feign自动中继吗?实现令牌中继InheritableThreadLocal实现令牌中继总结前言在Sprin
- 简要:EigenFace是基于PCA降维的人脸识别算法,PCA是使整体数据降维后的方差最大,没有考虑降维后类间的变化。 它是将图像
- ChatGPT的基本介绍ChatGPT是一个用来进行自然语言处理任务的预训练模型。要使用ChatGPT,需要了解以下几点:理解预训练模型:预
- 一、HttpBasic模式的应用场景HttpBasic登录验证模式是Spring Security实现登录验证最简单的一种方式,也可以说是最
- 我们都知道mybatis在进行参数判断的时候,直接可以用<if test=""></if> 就可
- Handler是什么?Handler 是一个可以实现多线程间切换的类,通过 Handler 可以轻松地将一个任务切换到 Handler 所在
- 前言单例模式最初的定义出现于《设计模式》(艾迪生维斯理, 1994):“保证一个类仅有一个实例,并提供一个访问它的全局访问点。”而我对单例的
- 1.获取屏幕大小,以合理设定 按钮 大小及位置 DisplayMetrics dm = new DisplayMetrics(); getW