动作条带片段标签和AdMob条带、片段、动作、标签

2023-09-09 22:01:11 作者:夜未眠

我有一个使用与标签结合片段的动作条的应用程序。 现在,我想在屏幕分成普通屏幕的顶部,并在底部的广告一个小酒吧: 左边是普通屏,标签及其片段占据整个屏幕。 我要的是右边的局面。的标签和碎片占用的红色部分,绿色部分是广告。 因此,红色部分应使房间的广告,我不希望覆盖的广告。

由于其中规定了动作条和标签的活动没有布局,我不能添加AD浏览报。

我怎样才能做到这一点?

修改 这是我实现我的应用程序。使用标签的动作条需要显示片段照顾,所以没有XML布局文件中使用的主要活动。

我的code: TestActivity.java

 公共类TestActivity扩展SherlockFragmentActivity {
    私人ActionBar的动作条;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        setupTabs(savedInstanceState);

        initAds();
    }

    私人无效setupTabs(包savedInstanceState){
        动作条= getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        addTab1();
        addTab2();
    }

    私人无效addTab1(){
        标签TAB1 = actionBar.newTab();
        tab1.setTag(1);
        串tabText =1;
        tab1.setText(tabText);
        tab1.setTabListener(新TabListener&其中; MyFragment>(TestActivity.this,1,MyFragment.class));

        actionBar.addTab(TAB1);
    }

    私人无效addTab2(){
        标签TAB1 = actionBar.newTab();
        tab1.setTag(2);
        串tabText =2;
        tab1.setText(tabText);
        tab1.setTabListener(新TabListener&其中; MyFragment>(TestActivity.this,2,MyFragment.class));

        actionBar.addTab(TAB1);
    }

    私人无效initAds(){
        //这里我想显示的广告,仅载入一次,就像Davek804说
    }
}
 

TabListener.java

 公共类TabListener<吨延伸SherlockFragment>实现com.actionbarsherlock.app.ActionBar.TabListener {
    私人最终SherlockFragmentActivity mActivity;
    私人最终字符串MTAG;
    私人最终类别< T> mClass;

    公共TabListener(SherlockFragmentActivity活动,字符串变量,类< T> CLZ){
        mActivity =活动;
        MTAG =标签;
        mClass = CLZ;
    }

    / *下面是每个ActionBar.TabListener回调* /

    公共无效onTabSelected(TAB键,FragmentTransaction英尺){
        SherlockFragment preInitializedFragment =(SherlockFragment)mActivity.getSupportFragmentManager()findFragmentByTag(MTAG)。

        //检查片段已初始化
        如果(preInitializedFragment == NULL){
            //如果没有,实例化并把它添加到活动
            SherlockFragment mFragment =(SherlockFragment)SherlockFragment.instantiate(mActivity,mClass.getName());
            ft.add(android.R.id.content,mFragment,MTAG);
        } 其他 {
            ft.attach(preInitializedFragment);
        }
    }

    公共无效onTabUnselected(TAB键,FragmentTransaction英尺){
        SherlockFragment preInitializedFragment =(SherlockFragment)mActivity.getSupportFragmentManager()findFragmentByTag(MTAG)。

        如果(preInitializedFragment!= NULL){
            //分离的片段,因为另一个被连接
            ft.detach(preInitializedFragment);
        }
    }

    公共无效onTabReselected(TAB键,FragmentTransaction英尺){
        //用户选择已经选择的选项卡。通常什么也不做。
    }
}
 
如何设置一个按钮或者一个标签对应多个动作

MyFragment.java

 公共类MyFragment扩展SherlockFragment {

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        返回inflater.inflate(R.layout.myfragment,集装箱,假);
    }

}
 

解决方案

创建一个包含AD浏览报定义的XML文件,并使用<包括> ,包括它的底部您的每一个片段的。

另外创建一个布局,你的标签吧。参看参考DOS:

  

要开始,你的布局必须包含的ViewGroup在其中   把与标签相关联的每个片段。要确保的ViewGroup有   资源ID,所以你可以从你的卡,交换code引用它。   或者,如果选项卡内容将填充活性布局   (不包括操作栏),那么你的活动并不需要一个布局   在所有(你甚至不需要调用的setContentView())。相反,你   可以将每一个片段的默认根ViewGroup中,你可以   请参阅随android.R.id.content ID(你可以看到这个ID中使用   样品code以下,在片段的交易)。

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.

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

How can I do this?

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.

My code: 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);
    }

}

解决方案

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:

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).