带有片段标签和 AdMob 的操作栏

Actionbar with Fragment tabs and AdMob(带有片段标签和 AdMob 的操作栏)

本文介绍了带有片段标签和 AdMob 的操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序将 ActionBar 与选项卡与 Fragments 结合使用.现在我想将屏幕分成顶部的普通屏幕和底部的小栏用于广告:
左边是普通屏幕,标签和它们的片段占据了整个屏幕.我想要的是右边的情况.标签和片段占据红色部分,绿色部分用于广告.所以红色部分应该为广告腾出空间,我不想覆盖广告.

I have an app that uses the ActionBar with tabs in combination with Fragments. Now I would like to separate the screen into the normal screen at the top, and a small bar at the bottom for the ads:
Left is the normal screen, the tabs and their Fragments take up the whole screen. What I want is the situation on the right. The tabs and Fragments take up the red part, the green part is for ads. So the red part should make room for the ads, I don't want to overlay the ads.

由于设置 ActionBar 和选项卡的 Activity 没有布局,我无法添加 AdView.

As the Activity which sets up the ActionBar and tabs has no layout, I'm not able to add the AdView.

我该怎么做?

编辑
这就是我实现我的应用程序的方式.带有选项卡的操作栏负责显示片段,因此在主 Activity 中不使用 xml 布局文件.

Edit
This is how I implemented my app. The actionbar with tabs takes care of showing the fragments, so no xml layout file is used in the main Activity.

我的代码: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);
    }

}

推荐答案

创建一个包含 AdView 定义的 XML 文件,并使用 <include> 将其包含在每个片段的底部.

Create an XML file containing the AdView definition and use <include> to include it at the bottom of each of your fragments.

或者创建一个布局和你的标签.参照.参考资料:

Alternatively create a layout and your tabs to it. Cf. the reference dos:

要开始,您的布局必须包含一个 ViewGroup,您可以在其中放置与选项卡关联的每个片段.确保 ViewGroup 有一个资源 ID,以便您可以从标签交换代码中引用它.或者,如果选项卡内容将填充活动布局(不包括操作栏),那么您的活动不需要布局完全没有(你甚至不需要调用 setContentView()).相反,你可以将每个片段放在默认的根 ViewGroup 中,您可以用android.R.id.content ID引用(可以看到这个ID用在下面的示例代码,在片段事务期间).

To get started, your layout must include a ViewGroup in which you place each Fragment associated with a tab. Be sure the ViewGroup has a resource ID so you can reference it from your tab-swapping code. Alternatively, if the tab content will fill the activity layout (excluding the action bar), then your activity doesn't need a layout at all (you don't even need to call setContentView()). Instead, you can place each fragment in the default root ViewGroup, which you can refer to with the android.R.id.content ID (you can see this ID used in the sample code below, during fragment transactions).

这篇关于带有片段标签和 AdMob 的操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带有片段标签和 AdMob 的操作栏

基础教程推荐