2014-01-28 47 views
3

我想更改tabwidget中選定選項卡的高度,如下圖所示。安卓更改tabwidget中選定選項卡的高度

我使用的方法:我使用了兩個圖像的第一個圖像有透明的部分在上面,第二個是完整的圖像,在選項卡上我改變了第一個圖像與第二個。但是這種方法在所有設備中都不起作用。我們必須創建具有相同高度的圖像,並將其轉換爲每個9個補丁。

該圖顯示了我確切需要的。 enter image description here

TabHost.TabSpec spec = mTabHost.newTabSpec(AppConstants.TAB_A); 

    spec.setContent(new TabHost.TabContentFactory() { 
     public View createTabContent(String tag) { 
      return findViewById(R.id.realtabcontent); 
     } 
    }); 
    spec.setIndicator(createTabView(R.drawable.hometabselection));//style applied here on each tab 
    mTabHost.addTab(spec); 

    spec     = mTabHost.newTabSpec(AppConstants.TAB_B); 
    spec.setContent(new TabHost.TabContentFactory() { 
     public View createTabContent(String tag) { 
      return findViewById(R.id.realtabcontent); 
     } 
    }); 
    spec.setIndicator(createTabView(R.drawable.realtabselection)); 
    mTabHost.addTab(spec); 
    spec  = mTabHost.newTabSpec(AppConstants.TAB_C); 
    spec.setContent(new TabHost.TabContentFactory() { 
     public View createTabContent(String tag) { 
      return findViewById(R.id.realtabcontent); 
     } 
    }); 

    spec.setIndicator(createTabView(R.drawable.topnewwsselection)); 

    mTabHost.addTab(spec); 

    spec     = mTabHost.newTabSpec(AppConstants.TAB_D); 
    spec.setContent(new TabHost.TabContentFactory() { 
     public View createTabContent(String tag) { 
      return findViewById(R.id.realtabcontent); 
     } 
    }); 

    spec.setIndicator(createTabView(R.drawable.treadingselection)); 

    mTabHost.addTab(spec); 

hometabselection.xml //在繪製其用於所有不同的標籤選擇

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
<item 
    android:state_selected="true" 
    android:drawable="@drawable/homeselected" />//full image 
<item 
    android:state_selected="false" 
    android:drawable="@drawable/homenormal" />//image with some portion transparent on top of that 
</selector> 

佈局

<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" > 
    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 
      <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="@dimen/fr_layout_width" 
      android:layout_height="@dimen/fr_layout_height" 
      android:layout_weight="0" /> 
     <FrameLayout 
      android:id="@+android:id/realtabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="@dimen/fr_layout_height" 
      android:layout_weight="1" /> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="@dimen/hdpi_tab_layout_height" 
      android:layout_weight="0" 
      android:divider="@drawable/tab_bg" 
      android:background="@android:color/transparent" 
      android:dividerPadding="@dimen/tab_divider_padding" 
      android:orientation="horizontal" /> 
    </LinearLayout> 
</TabHost> 
+0

你能提供你使用的代碼嗎? – nKn

+0

@NKN我更新了代碼。 –

+0

您可以添加如何動態設置其他可繪製的?我想你正在做一個onTabChangedListener。 – nKn

回答

2

我不知道這是否可行,但您可以嘗試保持已實施的方法,併爲TabHost另外實施onTabChangedListener,並在選擇選項卡時調整佈局的大小。我還沒有嘗試過,但我的想法應該是這樣的:

your_tabhost.setOnTabChangedListener(new OnTabChangeListener() { 
    @Override 
    public void onTabChanged(final String tabId) { 
    final TabHost th = (TabHost) findViewById(android.R.id.tabhost)); 
    final LinearLayout lLayout = (LinearLayout) th.getTabWidget().getChildTabViewAt(th.getCurrentTab()); 

    ViewGroup.LayoutParams curParams = lLayout.getLayoutParams(); 

    lLayout.setLayoutParams(new FrameLayout.LayoutParams(curParams.width, (int) (curParams.height * 1.20))); 
    } 
}); 

在這種情況下工作,但是,你會發現另外一個問題:你不知道哪個選項卡之前,選擇這個選擇。您需要將每個選項卡選項保存到類變量中,以便在選擇新選項卡後將其選定爲其默認高度之前選擇哪個選項。

+0

實施後,它給'''android.widget.FrameLayout $ LayoutParams不能轉換爲android.widget.LinearLayout $ LayoutParams「'這條線上的錯誤'LinearLayout.LayoutParams curParams = lLayout.getLayoutParams();' –

+0

我已經改變了代碼,請嘗試更新。 – nKn

+2

終於解決了幾個問題後解決'FrameLayout'被替換爲'Linearlayout' –

0

一種方式是一樣,你必須將該圖像轉換爲兩個不同大小的位圖並設置爲圖像視圖t是否被選中。

+0

這是OP所聲稱的目前所做的更多。 – ozbek

相關問題