Android开发之TabHost选项卡及相关疑难解决方法
作者:水中鱼之1999 发布时间:2022-01-10 06:30:57
本文实例分析了Android开发之TabHost选项卡及相关疑难解决方法。分享给大家供大家参考,具体如下:
前言:
虽然现在谷歌已经不推荐使用TabHost,但是初学者还是很有必要接触下这一成金的经典的,本文将介绍纤细介绍这一空间的使用,以及大家可能遇到的问题。注:文末给出完整实现代码
三个问题:
1. 无法显示TabHost
2. 添加图片 + 文字 无法同时
3. 说在最后:点击事件
4. 底部导航无法实现
现在
从问题出发:
问题一:无法显示 TabHost
很多人调用TabHost的方法是:
setContentView(R.layout.activity_main);
tabHost = getTabHost();
然后发现啥也没有,一脸蒙圈。。。 在这里建议大家采用遮掩的调用方法:
LayoutInflater.from(this).inflate(R.layout.activity_main,
tabHost.getTabContentView(), true);
成功后的页面:
注:UI 略丑请忽视
问题二:图片、文字无法同时添加
好了,很多人辛辛苦苦把界面搞出来了,可能想搞个底部菜单 加个图片,结果凉凉 半天搞不出来 ,这里介绍一个方法 ,由于TabHost本身图片、文字冲突 ,无法添加,这是我们就得把目光迁移到自定义view上:本段参考自:https://www.jb51.net/article/157914.htm
首先在/layout下建立自定义view名为:tab_indicator.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="64dip"
android:layout_weight="1"
android:orientation="vertical"
android:background="#45c0c0c0"
android:padding="5dp">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"
/>
</RelativeLayout>
接着,紧随其后在/drawable下添加:tab_info.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/find"
android:state_selected="true" />
<item android:drawable="@drawable/find1" />
</selector>
这些都搞定之后,就可以在活动中调用了:
首先在活动中先建立AddTab()方法:
private void AddTab(String label, int drawableId) {
Intent intent = new Intent(this, TextActivity.class);
TabHost.TabSpec spec = tabHost.newTabSpec(label);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(label);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
终于我们。。。:
成功了!!!
问题三:添加监听事件
这个无脑 只要 id 匹配就行了,直接上代码:
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
@Override
// tabId是newTabSpec参数设置的tab页名,并不是layout里面的标识符id
public void onTabChanged(String tabId) {
if (tabId.equals("tab1")) { //第一个标签
Toast.makeText(MainActivity.this, "点击标签页一", Toast.LENGTH_SHORT).show();
}else if (tabId.equals("tab2")) { //第二个标签
Toast.makeText(MainActivity.this, "点击标签页二", Toast.LENGTH_SHORT).show();
}else if (tabId.equals("tab3")) { //第三个标签
Toast.makeText(MainActivity.this, "点击标签页三", Toast.LENGTH_SHORT).show();
}
}
});
暂时能记起来的 疑难就这些了 如果还有请给我留言 我尽力解答。。
附上布局与实现:
布局:
<?xml version="1.0" encoding="utf-8" ?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scrollbarSize="100dp">
<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/tab01"
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="text11"
android:textSize="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text12"
android:textSize="20dp"/>
</LinearLayout>
<!--定义第二个标签页的内容-->
<LinearLayout
android:id="@+id/tab02"
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="text11"
android:textSize="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text12"
android:textSize="20dp"/>
</LinearLayout>
<!--定义第三个标签页的内容-->
<LinearLayout
android:id="@+id/tab03"
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="text11"
android:textSize="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text12"
android:textSize="20dp"/>
</LinearLayout>
</FrameLayout>
</TabWidget>
</LinearLayout>
</TabHost>
实现:
public class MainActivity extends TabActivity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.activity_main,
tabHost.getTabContentView(), true);
AddTab("tab1", R.drawable.tab_info);
AddTab("tab2", R.drawable.tab_info);
AddTab("tab3", R.drawable.tab_info);
//
//标签切换事件处理,setOnTabChangedListener
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
@Override
// tabId是newTabSpec参数设置的tab页名,并不是layout里面的标识符id
public void onTabChanged(String tabId) {
if (tabId.equals("tab1")) { //第一个标签
Toast.makeText(MainActivity.this, "点击标签页一", Toast.LENGTH_SHORT).show();
}else if (tabId.equals("tab2")) { //第二个标签
Toast.makeText(MainActivity.this, "点击标签页二", Toast.LENGTH_SHORT).show();
}else if (tabId.equals("tab3")) { //第三个标签
Toast.makeText(MainActivity.this, "点击标签页三", Toast.LENGTH_SHORT).show();
}
}
});
}
private void AddTab(String label, int drawableId) {
Intent intent = new Intent(this, TextActivity.class);
TabHost.TabSpec spec = tabHost.newTabSpec(label);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(label);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
ps:新建的layout和/drawable里的xml文件在问题给过,这里就不反复给了。
问题四:底部导航效果无法实现
底部导航的参见方法是把TabWidget放在FrameLayout后面,但是啧啧。。。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top">
中间内容前面给出 这里省略
</FrameLayout>
</LinearLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
</TabWidget>
</TabHost>
</RelativeLayout>
你会发现并没有什么 卵用 !!!呕!!,so:
百度了半天找不到问题所在,然后。。。修改下MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//原来
// tabHost = getTabHost();
// LayoutInflater.from(this).inflate(R.layout.activity_main,
// tabHost.getTabContentView(), true);
//修改后
setContentView(R.layout.activity_main);
tabHost = getTabHost();
tabHost.setup(this.getLocalActivityManager());
AddTab("tab1", R.drawable.tab_info);
AddTab("tab2", R.drawable.tab_info);
AddTab("tab3", R.drawable.tab_info);
//标签切换事件处理,setOnTabChangedListener
iniClick();
}
注:此处我已经将点击事件封装到方法中
最后:全剧终
哦,还没有且等我放下最后的图。。
啧啧,搞定
希望本文所述对大家Android程序设计有所帮助。
来源:https://blog.csdn.net/qq_43377749/article/details/85008914


猜你喜欢
- 问题描述在开发批量删除功能时,往往都是多条数据,所以前台需要传一个数组给后台,但是怎么在URL中绑定一个数组,同时在后台用@PathVari
- 一、Shiro简介:Apache Shiro是一个Java的安全(权限)框架。Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在J
- 前言我采用的是Camera来实现自定义相机的,如果你使用的是Camera2,那本文将不适用你。为了减少篇幅,本文主要讲解手动对焦的实现方式,
- 目前的设计中是支持单体声和立体声自动切换的。切换是需要在一定的条件下满足才会进行切换,切换的条件和电台的信号强度RSSI、信号稳定性CQI等
- 一、泛型的概念1.1 基础案例泛型在Java中的应用非常广泛,最常见则是在集合容器中,先看下基础用法:public class Generi
- 【引用】迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径。 它的主要特点是以起始点为中心
- 本文实例总结了C#中WinForm程序退出方法技巧。分享给大家供大家参考。具体分析如下:在c#中退出WinForm程序包括有很多方法,如:t
- 算法介绍概念 TF-IDF(term frequency–inverse document
- 本文实例为大家分享了Java实现医院管理系统的具体代码,供大家参考,具体内容如下1.开发工具NetBeans8.2Mysql5.7mysql
- 本文实例为大家分享了Android自定义带圆点的半圆形进度条,供大家参考,具体内容如下仅限用于半圆形,如须要带圆点的圆形进度条,圆点会出现错
- 文件目录结构文件目录结构很重要,特别注意的是rule文件要放在主启动类上一级位置,才能够扫描。写pom<dependencies>
- 前言对于线程安全,我们有说不尽的话题。大多数保证线程安全的方法是添加各种类型锁,使用各种同步机制,用限制对共享的、可变的类变量并发访问的方式
- 前言做APP应用开发的时候,用户头像肯定是必不可少的,但是90%以上的需求头像都是圆形的。那么,如何通过自定义View的方式实现圆形头像呢,
- 看书的时候被一段代码能凌乱啦,代码是这样的:package 继承;abstract class People {
- Android用SharedPreferences实现登录注册注销功能前言本文用SharedPreferences本地缓存账号信息来实现登录
- 本文实例讲述了Android编程基于自定义View实现绚丽的圆形进度条功能。分享给大家供大家参考,具体如下:本文包含两个组件,首先上效果图:
- 一、DurationDuration主要用来衡量秒级和纳秒级的时间,使用于时间精度要求比较高的情况。先来看看Duration的定义:publ
- 堆排序是一种树形选择排序方法,它的特点是:在排序的过程中,将array[0,...,n-1]看成是一颗完全二叉树的顺序存储结构,利用完全二叉
- (一)什么是微服务网关后端写完所有的微服务之后,最终是要交给前端去调用。我们都知道每个微服务都有各自的端口号,如果前端直接通过IP加端口的方
- 一:父级pom.xml文件 resources目录下新建指定文件夹,存放Spring配置文件<profiles> &