2012-08-14 102 views
2

我想創建操作欄中的自定義TabView的,是這樣的: enter image description here自定義操作欄TabView的

我得到了這樣的觀點:

enter image description here

我想改變的背景的選項卡查看指標,所以背景顏色變得不可見,並且我們在選定的tabview指標下有綠線。

這裏是我的代碼(我用默認TabView的):

public class BaseActivity extends Activity{ 

    ActionBar bar; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); setContentView(R.layout.base_activity); 

     bar = getActionBar(); 

     // Change action bar background 

     BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.background_actionbar)); background.setTileModeX(android.graphics.Shader.TileMode.REPEAT); 

     bar.setBackgroundDrawable(background); 

     bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     ActionBar.Tab tabA = bar.newTab().setText(getResources().getString(R.string.documents)); 

     ActionBar.Tab tabB = bar.newTab().setText(getResources().getString(R.string.videos)); 

     ActionBar.Tab tabC = bar.newTab().setText(getResources().getString(R.string.menu_settings)); 

     Fragment fragmentA = new DocumentsListFragment(); 

     Fragment fragmentB = new VideosListFragment(); 

     Fragment fragmentC = new UserAccountFragment(); 

     tabA.setTabListener(new MyTabsListener(fragmentA)); 

     tabB.setTabListener(new MyTabsListener(fragmentB)); 

     tabC.setTabListener(new MyTabsListener(fragmentC)); 

     bar.addTab(tabA); 

     bar.addTab(tabB); 

     bar.addTab(tabC); 
    } 


    protected class MyTabsListener implements ActionBar.TabListener 
    { 
     private Fragment fragment; 

     public MyTabsListener(Fragment fragment) { 
      this.fragment = fragment; } 

     @Override 

     public void onTabSelected(Tab tab, FragmentTransaction ft) 
     { 
      ft.replace(R.id.fragment_place, fragment, null); } 

     @Override 

     public void onTabReselected(Tab tab, FragmentTransaction ft) { 
      // TODO Auto-generated method stub 

     } 

     @Override 

     public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
      // TODO Auto-generated method stub 

     } 
    } 

} 

請,我怎麼能膨脹的TabView的指標。

+0

其實我想實現的觀點一樣,標籤視圖你有,但我不能成功,你能告訴你如何實現它,或給任何代碼示例?! – 2012-09-17 13:05:49

回答

4

添加這些都是styles.xml下值

<style name="Theme.AndroidDevelopers" parent="Theme.Sherlock.Light.ForceOverflow"> 
<item name=" android:actionBarItemBackground">@drawable/ad_selectable_background 
</item> 
    <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item> 
    <item name="actionBarTabStyle">@style/MyActionBarTabStyle</item> 
</style> 




<style name="MyActionBarTabStyle" parent="Widget.Sherlock.Light.ActionBar.TabBar"> 
    <item name="android:background">@drawable/actionbar_tab_bg</item> 
    <item name="android:gravity">center</item> 
    <item name="android:layout_gravity">center</item> 
    <item name="android:paddingLeft">10dp</item> 
    <item name="android:paddingRight">10dp</item> 
</style> 

這有點像你的標籤選擇:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/ad_tab_selected_pressed_holo"   android:state_selected="true"/> 
<item android:drawable="@android:color/transparent"/> 

+1

我得到htis錯誤:錯誤:檢索項目的父項時出錯:找不到與給定名稱匹配的資源 'Widget.Sherlock.Light.ActionBar.TabBar'。 我爲我的項目使用了android 3.0 – 2012-08-15 15:05:13

+0

您可能會錯過Sherlock Library:http://actionbarsherlock.com/ – 2013-12-31 08:30:09