2012-05-17 37 views
6

我正在使用從互聯網下載的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給了我一個主意。

+0

[你最好試試這個例子](http://stackoverflow.com/a/6992662/940096)我總是向所有人推薦這個例子。 – Praveenkumar

+0

http://stackoverflow.com/questions/5567532/android-ui-tabactivity-issue/5567823#5567823 ..試試這個職位 – Venky

+0

感謝您的回答,他們是相當有幫助的,我稍後可能會使用它。但是我想要做的是使用我從互聯網上下載的圖標設置這些標籤欄圖標,並且由於我沒有在資源中使用drawable-mdpi或drawable-ldpi,所以根據屏幕分辨率,它們不適合tabbar。我想在我的內存中使用圖標,而不是使用資源。例如使用createDrawableFromPath()。 – osayilgan

回答

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給了我一個主意。

+0

什麼是getTabWidget()? –

3

用於高密度(HDPI)屏幕標籤圖標尺寸: 完整資產:48×48像素 圖標:42×42像素

標記圖標尺寸中密度(MDPI)屏幕: 全部資產:32×32像素 圖標:28×28像素

爲低密度(LDPI)屏幕標籤圖標尺寸: 完整資產:24×24像素 圖標:22×22像素

可以看到這個: http://developer.android.com/guide/practices/ui_guidelines/icon_design_tab.html

+1

如果您使用應用程序資源中的圖標而非內部存儲器,則可以使用此選項,因爲下載的圖標只有一個維度。 – osayilgan

+0

@osayilgan從應用程序資源意味着來自drawable文件夾? – user3233280

+0

@ user3233280是的,資源意味着與您的應用程序捆綁在一起的任何東西。它可以是「Res」或「Assets」文件夾中的任何內容。 – osayilgan