2010-12-16 109 views
7

我需要縮放我的TabWidget背景圖片,以保持寬高比。
我正在使用TabWidget的TabHost。然後我使用setBackgroundDrawable來設置圖像。TabHost/TabWidget - 縮放背景圖像?

我在這裏發現了一個很接近的答案 - Background in tab widget ignore scaling。但是,我不確定在哪裏添加新的Drawable代碼。 (使用HelloTabWidget示例,我的模塊都沒有使用RelativeLayout,並且我沒有看到「tabcontent」的任何佈局。)

我還發現此線程 - Android: Scale a Drawable or background image?。據它說,這聽起來像我將不得不預先縮放我的圖像,這打破了使它們可擴展的整體目的。

我還發現另一個線程,其中有人劃分了Drawable類,所以它不會縮放,或者它可以正確縮放。我現在找不到它,但是當你只需要做一些像mTab.setScaleType(centerInside)這樣簡單的工作時,這看起來就像是一個很好的選擇。

這裏是我的代碼:

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/main_background"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/>   
    <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0"/> 
    </LinearLayout> 
</TabHost> 

主要活動:

 tabHost.setOnTabChangedListener(new OnTabChangeListener() { 
      TabHost changedTabHost = getTabHost(); 
      TabWidget changedTabWidget = getTabWidget(); 
      View changedView = changedTabHost.getTabWidget().getChildAt(0); 

      public void onTabChanged(String tabId) { 
       int selectedTab = changedTabHost.getCurrentTab(); 
       TabWidget tw = getTabWidget(); 

       if(selectedTab == 0) { 
        //setTitle("Missions Timeline"); 
        View tempView = tabHost.getTabWidget().getChildAt(0); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_timeline_on)); 
        tempView = tabHost.getTabWidget().getChildAt(1); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_map_off)); 
        tempView = tabHost.getTabWidget().getChildAt(2); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_search_off)); 
        tempView = tabHost.getTabWidget().getChildAt(3); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_news_off)); 
        tempView = tabHost.getTabWidget().getChildAt(4); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_license_off)); 
          //ImageView iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon); 
          //iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_timeline_on)); 
          //iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon); 
          //iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_off)); 
        } else if (selectedTab == 1) { 
         //setTitle("Spinoffs Around You"); 
         View tempView = tabHost.getTabWidget().getChildAt(0); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_timeline_off)); 
         tempView = tabHost.getTabWidget().getChildAt(1); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_map_on)); 
         tempView = tabHost.getTabWidget().getChildAt(2); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_search_off)); 
         tempView = tabHost.getTabWidget().getChildAt(3); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_news_off)); 
         tempView = tabHost.getTabWidget().getChildAt(4); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_license_off)); 
} 

我也試過9patch圖像,但是它們將是太小了。

那麼,最好的方法是什麼呢?

回答

0

我懷疑你努力尋找開箱即用的方法來執行背景圖像縮放的原因是,它通常不被視爲良好的做法。圖像縮放往往會引入工件和條紋,這會使您的界面看起來很討厭。這個例外是在一個Imageview中,但我認爲這個類提供的伸縮支持更多的是支持像照片或網頁內容(即你的應用沒有附帶的圖片)。

您的用戶界面佈局的設計應該使得任何基於資源的背景圖像都應該按照設備密度/屏幕尺寸的正確分辨率進行預先縮放。換句話說,您應該使用Supporting Multiple Screens開發指南中概述的資源文件夾命名約定來提供每個drawable的多個版本以滿足多個屏幕密度和大小。

然後應佈置用戶界面,以便設備之間屏幕尺寸的任何細微差異都可以處理,而無需縮放其包含的視圖的背景圖像。很顯然,我不知道你提出的用戶界面是什麼樣子,所以很難提出具體的建議,但通常我使用簡單的背景塊顏色或動態填充視圖之間的空間。

但是,如果你真的確定你想要擴展你的背景圖片,儘管上面講道:-),爲什麼不嘗試使用ScaleDrawable來包裝你現有的drawable?

如果您知道這兩個視圖和背景圖像繪製的高度和寬度,那麼你可以這樣做:

Drawable backgroundDrawable = getResources().getDrawable(R.drawable.tab_license_off); 
tempView.setBackgroundDrawable(
    new ScaleDrawable(
     backgroundDrawable, 
     Gravity.CENTER, 
     tempView.getWidth()/backgroundDrawable.getIntrinsicWidth(), 
     tempView.getHeight()/backgroundDrawable.getIntrinsicHeight());