Android tabHost和tabWidget图标问题

6

我正在开发一个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给了我一个想法。


你最好尝试这个例子:https://dev59.com/D3E85IYBdhLWcg3wpFOu#6992662。我总是建议所有人都尝试这个例子。 - Praveenkumar
谢谢你的回答,它们非常有帮助,我以后可能会用到。但是我想做的是使用我从互联网上下载的图标来设置这些选项卡栏图标,由于我在资源中没有使用drawable-mdpi或drawable-ldpi,所以它们不适合屏幕分辨率的选项卡栏。我想使用内部存储器中的图标,例如使用createDrawableFromPath()函数。 - osayilgan
@Paramone 谢谢,我会看一下。 - osayilgan
这个解决方案的问题在于,如果您想要为选项卡使用 Holo 主题,您将失去默认分隔符和下划线可见性。 - IgorGanapolsky
现在的解决方法是使用带有LinearLayout标签的工具栏(Material Design)。请参阅GitHub上的GoogleIO示例代码,或SDK 21中的示例。 - IgorGanapolsky
显示剩余5条评论
2个回答

11

不再使用之前的代码往选项卡主机中添加对象,现在我使用下面的代码,它正常工作。这两种方法的区别在于,新方法使用了一个布局,其中包含图像视图和文本视图来指示图标和下方的文本以设置意图的指示器。因此,通过这种方式,我可以从图像视图调用方法,使图像适合在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 给了我一个思路。


什么是getTabWidget()? - IgorGanapolsky

3
高密度(hdpi)屏幕的选项卡图标尺寸: 完整资源:48 x 48 像素 图标:42 x 42 像素
中等密度(mdpi)屏幕的选项卡图标尺寸: 完整资源:32 x 32 像素 图标:28 x 28 像素
低密度(ldpi)屏幕的选项卡图标尺寸: 完整资源:24 x 24 像素 图标:22 x 22 像素
您可以访问此链接了解更多信息: http://developer.android.com/guide/practices/ui_guidelines/icon_design_tab.html

1
如果您使用应用程序资源中的图标而不是内部存储器中的图标,则可以使用此选项,因为下载的图标只有一个维度。 - osayilgan
@osayilgan 从应用程序资源中获取意味着从drawable文件夹中获取吗? - user3233280
@user3233280 是的,资源指的是与您的应用程序捆绑在一起的任何内容。它可以是“Res”或“Assets”文件夹中的任何内容。 - osayilgan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接