2013-03-29 120 views
2

我在使用我的選項卡設置設計時遇到問題。所以我爲tabHost中添加的每個tabspec創建了選擇器。下面是它的樣子:Android TabHost選項卡背景較小

enter image description here

因此,這裏是我的標籤活動:

public class TabLayouts extends TabActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabhost); 

     TabHost tabHost = getTabHost(); 
     tabHost.getTabWidget().setStripEnabled(false); 

     TabSpec latest = tabHost.newTabSpec(getString(R.string.latest_title)); 
     tabHost.setBackgroundResource(R.drawable.tabbar); 

     // setting Title and Icon for the Tab 
     latest.setIndicator(getString(R.string.latest_title), getResources().getDrawable(R.drawable.latest_albums_sel)); 
     Intent latestAlbums = new Intent(this, LatestAlbums.class); 
     latest.setContent(latestAlbums); 

     TabSpec favorites = tabHost.newTabSpec(getString(R.string.favorites_title)); 
     favorites.setIndicator(getString(R.string.favorites_title), getResources().getDrawable(R.drawable.favorites_sel)); 
     Intent favoritesInt = new Intent(this, Favorites.class); 
     favorites.setContent(favoritesInt); 

     TabSpec downloaded = tabHost.newTabSpec(getString(R.string.downloaded_title)); 
     downloaded.setIndicator(getString(R.string.downloaded_title), getResources().getDrawable(R.drawable.downloaded_sel)); 
     Intent downloadedIntent = new Intent(this, Downloaded.class); 
     downloaded.setContent(downloadedIntent); 

     tabHost.addTab(latest); 
     tabHost.addTab(favorites); 
     tabHost.addTab(downloaded); 
    } 
} 

而我的選擇之一:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- PRESSED TAB --> 
    <item android:top="0dp" android:left="0dp" android:right="0dp" android:bottom="0dp" 
     android:state_pressed="true" 
     android:drawable="@drawable/menu3_pr" 
     /> 
    <!-- INACTIVE TABS --> 
    <item android:top="0dp" android:left="0dp" android:right="0dp" android:bottom="0dp" 
     android:state_selected="false" 
     android:state_focused="false" 
     android:state_pressed="false" 
     android:drawable="@drawable/menu3_nr" 
     /> 
    <!-- ACTIVE TAB --> 
    <item android:top="0dp" android:left="0dp" android:right="0dp" android:bottom="0dp" 
     android:state_selected="true" 
     android:state_focused="false" 
     android:state_pressed="false" 
     android:drawable="@drawable/menu3_pr" 
     /> 
    <!-- SELECTED TAB --> 
    <item android:top="0dp" android:left="0dp" android:right="0dp" android:bottom="0dp" 
     android:state_focused="true" 
     android:state_selected="true" 
     android:state_pressed="false" 
     android:drawable="@drawable/menu3_pr" 
     /> 
</selector> 

而且我tabhost佈局:

<?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"> 
    <RelativeLayout 
     android:background="@drawable/tabbar" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <FrameLayout 
      android:background="@drawable/background" 
      android:layout_above="@android:id/tabs" 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent"/> 
     <TabWidget 
      android:showDividers="none" 
      android:layout_alignParentBottom="true" 
      android:id="@android:id/tabs" 
      android:scaleY="0.8" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
    </RelativeLayout> 
</TabHost> 

任何人都可以幫我解決這個問題嗎?

+0

請參考[鏈接](https://github.com/AdilSoomro/Iphone-Tab-in-Android)希望這會幫助你。 –

回答

5

這是簡單的技術,你應該使用.setIndicator()一個參數傳遞是

latest.setIndicator(getString(R.string.latest_title)). 

上面使用的是

.setIndicator(getString(R.string.latest_title), getResources().getDrawable(R.drawable.latest_albums_sel)) 

現在刪除是

getResources().getDrawable(R.drawable.latest_albums_sel)

第二個參數

from your cod e和當您將添加所有選項像

​​

使用下面

tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.latest); 
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.favorites); 
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.downloaded); 

代碼和你做:) 幫我希望你能解決你的問題也是如此。

+1

它也解決了我的問題 –

+1

我一直在努力解決這個問題很長..簡單而有用的技術 – png