简体   繁体   English

Android-以XML定义的AddContentView

[英]Android - AddContentView defined in XML

AdMob documentation to integrate it on Android instructs to create the following XML, if we want to create the view via XML: 如果我们想通过XML创建视图,则将其集成到Android上的AdMob文档指示创建以下XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <com.google.android.gms.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="MY_AD_UNIT_ID"
                         ads:adSize="BANNER"/>
</LinearLayout>

And set it as content view to Activity: 并将其设置为活动的内容视图:

public class BannerExample extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Look up the AdView as a resource and load a request.
    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
  }
}

But in my case (I use cocos2d-x 3) I have already a subclass of Activity and I just need to add that view to my existing content, not set content view as it is done in the code above. 但是就我而言(我使用cocos2d-x 3),我已经有一个Activity的子类,我只需要将该视图添加到我现有的内容中,而不是像上面的代码中那样设置内容视图。 How I can addContentView the view that is defined in XML? 如何添加ContentView XML定义的视图?

When you call. 你打电话时。

setContentView(R.layout.main);

in onCreate it automatically inflates the view for you so you can start assigning your views from the xml to your own variables like such: 在onCreate中,它会自动为您放大视图,因此您可以开始将xml中的视图分配给自己的变量,例如:

AdView adView = (AdView) findViewById(R.id.adView);

Make sure that the xml layout you defined at the top is the same that is being passed in setContentView(). 确保您在顶部定义的xml布局与setContentView()中传递的布局相同。 In this case R.layout.main should contain a View with id adView of AdView type. 在这种情况下,R.layout.main应包含ID为adView且AdView类型为ID的View。

In other words, R.layout.main should be: 换句话说,R.layout.main应该是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <com.google.android.gms.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="MY_AD_UNIT_ID"
                         ads:adSize="BANNER"/>
</LinearLayout>

If what you want however, is inflate and use main layout and add the AdView, you must inflate the view yourself and add it to some element in main layout. 但是,如果要充气并使用主布局并添加AdView,则必须自己充气视图并将其添加到主布局中的某些元素。 Like such: 像这样:

RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);

References: How to inflate one view with a layout 参考: 如何使用布局使一个视图膨胀

You can simply add 您可以简单地添加

<com.google.android.gms.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="MY_AD_UNIT_ID"
                     ads:adSize="BANNER"/>

to your existing XML layout file. 到您现有的XML布局文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM