2013-11-03 374 views
8

我使用ActionBar.Tab setCustomView()方法,用這個佈局:的Android ActionBar.Tab setCustomView()不FILL_PARENT

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/background_grey" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="Test Tab" 
     android:textColor="@color/background_dark_green"/> 

</RelativeLayout> 

這是我的功能設置動作條:

public void setActionBar() 
{ 
    ActionBar actionBar = getSupportActionBar(); 
    //actionBar.hide(); 
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    actionBar.setDisplayShowHomeEnabled(false); 
    actionBar.setDisplayShowTitleEnabled(false);   
    //set action bar navigation mode 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);   
    //set tabs  
    //home tab 
    Tab tab = actionBar.newTab().setText(TAB_HOME).setTabListener(new PicoTabListener<StartFragment>(this, StartFragment.class));  
    tab.setCustomView(R.layout.tab_background); 
    actionBar.addTab(tab); 
    //events tab 
    tab = actionBar.newTab().setText(TAB_EVENTS).setTabListener(new PicoTabListener<EventsFragment>(this, EventsFragment.class)); 
    actionBar.addTab(tab);  
    //enter event code 
    tab = actionBar.newTab().setText(TAB_CODE).setTabListener(new PicoTabListener<EnterCodeFragment>(this, EnterCodeFragment.class)); 
    actionBar.addTab(tab);  
} 

和我的活動佈局:

<?xml version="1.0" encoding="utf-8"?> 
<!-- This is the main layout of the application --> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_basic_root" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/background_dark_green" > 

</RelativeLayout> 

結果接縫看起來像這樣(帶有灰色背景的左上角的標籤): enter image description here

如何讓我的自定義視圖填充整個選項卡並正常工作?

我使用的是支持包V7的Android 2.3

+0

請您發表您的活動XML佈局? – nio

+0

完成。我已更新我的問題 –

回答

0

您有actionbarcompat的setCustomView處理記住

保持這看上去好像是根元素的填充,寬度,高度時,依靠android:marginandroid:padding和邊際,被忽略。

3

恰好碰到了這個自己,並想出解決方案。你應該爲tabview創建一個清除背景和填充的風格,並在你的主題中使用它。

styles.xml:

<style name="Custom.ActionBar.TabView.Empty" parent="@style/Widget.AppCompat.ActionBar.TabView"> 
    <item name="android:background">@null</item> 
    <item name="android:padding">0dp</item> 
</style> 

的themes.xml:

<style name="Theme.Custom" parent="@style/Theme.AppCompat.Light"> 
    <item name="android:actionBarTabStyle">@style/Custom.ActionBar.TabView.Empty</item> 
    <item name="actionBarTabStyle">@style/Custom.ActionBar.TabView.Empty</item> 
</style> 
+0

不適合我。任何替代方案? –

+0

嘗試以編程方式將選項卡中的RelativeLayout的'LayoutParams'設置爲'MATCH_PARENT',以使您的自定義視圖佔據選項卡中的所有可用空間。 我寫了一篇關於在標籤中添加徽章的博客文章,因爲我也有一些問題:[http://kevinpelgrims.com/blog/2014/06/24/adding-a-badge- [1] [1]:http://kevinpelgrims.com/blog/2014/06/24/adding-a-badge-to-an-actionbar-tab – kevinpelgrims

1

對每一個創建標籤對象只需添加的LayoutParams:

..  
tab.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)); 
actionBar.addTab(tab); 
+0

謝謝,你救了我!雖然我的情況有點不同 - 我試圖設置ActionBar的自定義視圖(它的根)layout_width =「fill_parent」,但它不起作用(就像它被設置爲「wrap_content」一樣)。 'cView.setLayoutParams(RelativeLayout.Layout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT));'解決了問題 – Mixaz

+0

我忘記提及我在'actionBar後面調用'cView.setLayoutParams(...)'。 setCustomView(CVIEW);' – Mixaz