gpt4 book ai didi

Android tabHost 和 tabWidget 图标问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:11 24 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,它使用从互联网下载的选项卡主机图标,图标大小为 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。任何人都知道问题是什么?

Here is the tabhost with icons from resources

Here is the tabhost with icons downloaded from internet

解决方案:

现在我使用下面的代码,而不是使用我以前的代码将对象添加到选项卡宿主,它工作得很好。这两者之间的区别是新的是使用具有 ImageView 和 TextView 的布局来指示图标和下面的文本来设置 Intent 的指示器。因此,通过这种方式,我可以从 ImageView 中调用该方法,使图像适合其在 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);
}

这是带有 ImageView 和 TextView 的布局。

<?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 给我一个想法。

最佳答案

现在我使用下面的代码,而不是使用我以前的代码将对象添加到选项卡宿主,它工作得很好。这两者之间的区别是新的是使用具有 ImageView 和 TextView 的布局来指示图标和下面的文本来设置 Intent 的指示器。因此,通过这种方式,我可以从 ImageView 中调用该方法,使图像适合其在 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);
}

这是带有 ImageView 和 TextView 的布局。

<?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 给我一个想法。

关于Android tabHost 和 tabWidget 图标问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10631627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com