2012-06-08 104 views
3

我有一個應用程序使用帶有與片段組合的選項卡的ActionBar。 現在我想將屏幕分爲最上面的普通屏幕和底部的小廣告欄: enter image description here
左邊是正常屏幕,選項卡和它們的碎片佔據了整個屏幕。 我想要的是右邊的情況。選項卡和碎片佔據紅色部分,綠色部分用於廣告。 所以紅色部分應該爲廣告騰出空間,我不想疊加廣告。帶有片段選項卡和AdMob的操作欄

作爲設置ActionBar和選項卡沒有佈局的活動,我無法添加AdView。

我怎樣才能做到這一點?

編輯
這是我如何實現我的應用程序。帶有製表符的操作欄負責顯示片段,因此在主Activity中不使用xml佈局文件。

我的代碼: TestActivity.java

public class TestActivity extends SherlockFragmentActivity { 
    private ActionBar actionBar; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setupTabs(savedInstanceState); 

     initAds(); 
    } 

    private void setupTabs(Bundle savedInstanceState) { 
     actionBar = getSupportActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     addTab1(); 
     addTab2(); 
    } 

    private void addTab1() { 
     Tab tab1 = actionBar.newTab(); 
     tab1.setTag("1"); 
     String tabText = "1"; 
     tab1.setText(tabText); 
     tab1.setTabListener(new TabListener<MyFragment>(TestActivity.this, "1", MyFragment.class)); 

     actionBar.addTab(tab1); 
    } 

    private void addTab2() { 
     Tab tab1 = actionBar.newTab(); 
     tab1.setTag("2"); 
     String tabText = "2"; 
     tab1.setText(tabText); 
     tab1.setTabListener(new TabListener<MyFragment>(TestActivity.this, "2", MyFragment.class)); 

     actionBar.addTab(tab1); 
    } 

    private void initAds(){ 
     //Here I want to display the ad, only loading once, Just like Davek804 said 
    } 
} 

TabListener.java

public class TabListener<T extends SherlockFragment> implements com.actionbarsherlock.app.ActionBar.TabListener { 
    private final SherlockFragmentActivity mActivity; 
    private final String mTag; 
    private final Class<T> mClass; 

    public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) { 
     mActivity = activity; 
     mTag = tag; 
     mClass = clz; 
    } 

    /* The following are each of the ActionBar.TabListener callbacks */ 

    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag); 

     // Check if the fragment is already initialized 
     if (preInitializedFragment == null) { 
      // If not, instantiate and add it to the activity 
      SherlockFragment mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName()); 
      ft.add(android.R.id.content, mFragment, mTag); 
     } else { 
      ft.attach(preInitializedFragment); 
     } 
    } 

    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag); 

     if (preInitializedFragment != null) { 
      // Detach the fragment, because another one is being attached 
      ft.detach(preInitializedFragment); 
     } 
    } 

    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     // User selected the already selected tab. Usually do nothing. 
    } 
} 

MyFragment.java

public class MyFragment extends SherlockFragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.myfragment, container, false); 
    } 

} 
+0

所以,你要AdView中可見,無論哪個選項卡的用戶嗎?基本上,這是不正確的,但你想要的是,adView基本上是整個主佈局,但它只佔據底部?因此,當用戶切換選項卡時,紅色發生變化,但綠色保持不變(不是每個選項卡上的新版綠色)? – Davek804

+0

Davek804沒錯。 – nhaarman

回答

4

創建包含AdView中定義的XML文件,並使用<include>包括我t在每個碎片的底部。

或者創建一個佈局和你的標籤。參看參考DOS:

要開始,你的佈局必須包含在其中 你的地方與標籤相關聯的每個片段的ViewGroup。確保ViewGroup有一個 資源ID,以便您可以從您的製表符交換代碼中引用它。 或者,如果標籤內容將填充活動佈局 (不包括操作欄),那麼您的活動根本不需要佈局 (您甚至不需要調用setContentView())。相反,您可以將每個片段放置在默認根ViewGroup中,您可以使用android.R.id.content ID引用 引用(在片段事務處理過程中,您可以在下面的示例代碼中看到此ID用於此示例代碼)。

+0

這會導致廣告在切換標籤時重新加載,而這不是我想要的。 – nhaarman

+1

您可能想在問題中這樣說。然後只需創建一個RelativeLayout並將AdView錨定到底部。然後添加你的片段不是'android.R.id.content',而是添加到佈局中的一個容器(FrameLayout將會這樣做)。參看參考:http://developer.android.com/guide/topics/ui/actionbar.html#Tabs –

+0

我認爲這很明顯。無論如何,這工作,謝謝! – nhaarman

相關問題