【问题标题】:Adding image to custom tab将图像添加到自定义选项卡
【发布时间】:2011-05-12 05:47:10
【问题描述】:
我使用this 示例来创建自定义选项卡。我不知道如何在标签视图中添加图像。
我尝试在 tabs_bg.xml 中添加ImageView 元素,但不显示图像,只显示文本
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgTab"
android:background="@drawable/img"/>
有人可以帮我解决这个问题吗?
谢谢
【问题讨论】:
标签:
android
android-tabhost
android-custom-view
【解决方案1】:
您必须为ImageView 指定android:src 属性:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgTab"
android:src="@drawable/img"/>
如果没有内容(src),它的大小将是 0x0(由于wrap_content)。
指定标签视图的指示符(标题)是由java代码制作的,通常是链式的:
myTabHost.newTabSpec(tag).setIndicator(myTab).setContent(intent);
其中myTabHost 是您的TabHost 实例,myTab 是一个View 实例,将用作此选项卡的标题。
您可以创建自己的选项卡:在 xml 中定义其布局并在其上添加您需要的所有视图(图像、文本...)。
有关参考(完整示例),请参阅update part of this answer。
layout/tab.xml 文件包含选项卡标题的布局(也包括一个图标)。