Android TabHost选项卡标签图标始终不出现的解决方法

作者:张智清 时间:2021-08-25 14:32:59 

本文实例分析了Android TabHost选项卡标签图标始终不出现的解决方法。分享给大家供大家参考,具体如下:

在学习Android TabHost布局过程中,很多教程告诉我,这样来显示选项卡标签的图标和文字:


TapSpec spec1 = tabHost.newTabSpec("tab 1");
spec1.setIndicator("选项卡一", getResources().getDrawable(R.drawable.tab_icon));
spec1.setContent(R.id.tab1);
tabHost.addTab(spec1);

折腾来折腾去,setIndicator(label, drawable)这个方法始终不能将标题文字与图标一起显示出来,只有文字标题。

在没将电脑砸了之前,通过万能的stackoverflow.com终于知道确切答案以及相应方法了:
http://stackoverflow.com/questions/10745092/icon-in-tab-is-not-showing-up

其实就是SDK 4.03(冰激凌)下:只有文字标题显示,图标是不显示的。如果将文字标题设置为空字符串,则此时图标可显示。

对于冰激凌下两全其美的方法,只能是自定义标签卡布局,创建一个包含ImageView和TextView组件的界面布局文件 tab_indicator.xml(layout/tab_indicator.xml),然后用setIndicator(View view)方法来设置TabSpec的界面布局。


<?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="@drawable/tab_indicator"
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文件,用来指示Tab图标的各状态。


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tab_info_dark"
android:state_selected="true" />
<item android:drawable="@drawable/tab_info_light" />
</selector>

现在就可以通过下面的代码将我们自定义的视图作为一个indicator配置给TapSpec对象。


private void addTab(String label, int drawableId) {
Intent intent = new Intent(this, MockActivity.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);
}

像以下方式那样调用上面那自定义addTab方法


tabHost = getTabHost(); //tabHost is a private field
addTab("First", R.drawable.tab_info);
addTab("Second", R.drawable.tab_info);
addTab("Third", R.drawable.tab_info);

注意:当用自定义视图的indicator来添加Tab时,要将strip_enabled属性设置为false。若要兼顾底部strip,那在添加最后一个Tab后设置getTabWidget().setStripEnabled(true);

希望本文所述对大家Android程序设计有所帮助。

来源:https://www.cnblogs.com/lovecode/articles/2652510.html

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

猜你喜欢

  • Android中使用itemdecoration实现时间线效果

    2021-10-08 12:15:49
  • Android类加载流程分析

    2023-05-30 11:13:44
  • C#中ToString数据类型格式大全(千分符)

    2023-10-03 05:08:48
  • Android自定义View实现渐变色进度条

    2022-11-25 08:27:17
  • 浅析Java中JSONObject和JSONArray使用

    2022-06-05 14:58:30
  • Java实现人机猜拳游戏

    2023-10-16 08:47:56
  • java8 stream自定义分组求和并排序的实现

    2022-09-12 04:08:26
  • unity实现手游虚拟摇杆

    2021-11-23 07:16:44
  • Java中websocket消息推送的实现代码

    2023-06-02 09:26:56
  • Java设计模式中的观察者模式

    2021-08-22 01:27:20
  • 关于Java实现HttpServer模拟前端接口调用

    2021-07-10 07:44:17
  • Android IdleHandler使用方法详解

    2023-11-17 20:48:11
  • 一起聊聊Java中13种锁的实现方式

    2022-11-23 15:26:00
  • Java字节流和字符流总结IO流!

    2023-10-21 13:06:02
  • C# Winform实现石头剪刀布游戏

    2022-03-17 17:05:14
  • C#下listview如何插入图片

    2023-06-18 11:45:44
  • 详解C#多线程之线程同步

    2023-10-09 02:23:21
  • C++实现日期类的示例详解

    2022-07-29 04:19:24
  • Ubuntu16.04 LTS 下安装 Android Studio 2.2.2 的详细步骤

    2022-08-11 09:59:22
  • C#中使用反射遍历一个对象属性及值的小技巧

    2021-12-10 18:15:43
  • asp之家 软件编程 m.aspxhome.com