我正在使用從互聯網下載的Tab主機圖標和圖標大小爲30x30的Android應用程序。Android tabHost和tabWidget圖標問題
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
SOLUTION:
而是將對象添加到選項卡主與我以前的代碼,現在我用下面的代碼,它工作得很好。這兩者之間的區別是新的使用佈局,其中有圖像視圖和文本視圖來指示圖標和下面的文本來設置意圖的指示器。因此,通過這種方式,我可以從圖像視圖調用該方法,使圖像適合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給了我一個主意。
[你最好試試這個例子](http://stackoverflow.com/a/6992662/940096)我總是向所有人推薦這個例子。 – Praveenkumar
http://stackoverflow.com/questions/5567532/android-ui-tabactivity-issue/5567823#5567823 ..試試這個職位 – Venky
感謝您的回答,他們是相當有幫助的,我稍後可能會使用它。但是我想要做的是使用我從互聯網上下載的圖標設置這些標籤欄圖標,並且由於我沒有在資源中使用drawable-mdpi或drawable-ldpi,所以根據屏幕分辨率,它們不適合tabbar。我想在我的內存中使用圖標,而不是使用資源。例如使用createDrawableFromPath()。 – osayilgan