我正在开发一个Android应用程序,使用从互联网下载的Tab Host图标,图标大小为30x30。
for(int i = 0; i < titleNames.size(); i++)
{
intent = new Intent().setClass(context, Gallery.class);
sp = tabHost.newTabSpec(titleNames.get(i)).setIndicator(titleNames.get(i), res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(sp);
}
如果我使用上面的代码(来自资源的图标)来设置指示器文本和图标,它可以很好地工作,并且图标适合选项卡小部件。
for(int i = 0; i < titleNames.size(); i++)
{
intent = new Intent().setClass(context, Gallery.class);
sp = tabHost.newTabSpec(titleNames.get(i)).setIndicator(titleNames.get(i), Drawable.createFromPath(path+iconNames.get(i))).setContent(intent);
tabHost.addTab(sp);
}
但是,如果我使用这段代码(从互联网下载的图像并保存在内部存储器中),而不是之前的代码,则图标看起来很小,即使两个图标的高度和宽度值都相同。我在从互联网下载图标时没有缩放它们,并将它们保存为PNG格式。有人知道问题出在哪里吗? 这是带有资源图标的选项卡宿主 这是从互联网下载的图标的选项卡宿主 解决方案: 现在,我使用下面的代码来向选项卡宿主添加对象,它非常有效。这两者之间的区别是新代码使用一个布局,其中包含图像视图和文本视图以指示图标和下面的文本以设置意图的指示器。因此,在这种方式下,我能够从图像视图调用方法,使图像适应在xml文件中定义的边界。
以下是使用View的方法。
private void addTab(String labelId, Drawable drawable, Class<?> c) {
tabHost = getTabHost();
intent = new Intent(this, c);
spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageDrawable(drawable);
icon.setScaleType(ImageView.ScaleType.FIT_CENTER);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
这是带有图像视图和文本视图的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="55" >
<ImageView
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="0dp"
android:src="@drawable/icon"
android:adjustViewBounds="false"
android:layout_weight="30"
/>
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:gravity="center_horizontal"
/>
</LinearLayout>
感谢@Venky和@SpK给了我一个想法。