【问题标题】:LayoutParams require API 19?LayoutParams 需要 API 19?
【发布时间】:2014-06-02 15:00:23
【问题描述】:

我正在使用 Andengine 并想添加一个 Admob 横幅。我找到了这段代码,它运行良好。但是使用此代码,LayoutParams 至少需要 API 19。我想变得更低,比如说 8 或类似的东西。我可以用什么代替?

@Override protected void onSetContentView() {
    // CREATING the parent FrameLayout //
    final FrameLayout frameLayout = new FrameLayout(this);

    // CREATING the layout parameters, fill the screen //
    final FrameLayout.LayoutParams frameLayoutLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                                         FrameLayout.LayoutParams.MATCH_PARENT);

    // CREATING a Smart Banner View //
    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId("ca-app-pub-2181015300018417/9550656088");

    // Doing something I'm not 100% sure on, but guessing by the name //
    adView.refreshDrawableState();
    adView.setVisibility(AdView.VISIBLE);

    // ADVIEW layout, show at the bottom of the screen //
    final FrameLayout.LayoutParams adViewLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                                         FrameLayout.LayoutParams.WRAP_CONTENT,
                                         Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);

    // REQUEST an ad (Test ad) //
    AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("AB38B36BA72798C94A9C9329007FD4B0")
    .build();
    adView.loadAd(adRequest);

    // RENDER the add on top of the scene //
    this.mRenderSurfaceView = new RenderSurfaceView(this);
    mRenderSurfaceView.setRenderer(mEngine, this);

    // SURFACE layout ? //
    final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
            new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());

    // ADD the surface view and adView to the frame //
    frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
    frameLayout.addView(adView, adViewLayoutParams);

    // SHOW AD //
    this.setContentView(frameLayout, frameLayoutLayoutParams);

} // onSetContentView() 结束 //

【问题讨论】:

  • 阅读文档。只有 1 个构造函数是 api19,其余的都是 api 1。你可能并不真的需要那个构造函数。
  • 根据文档,它不需要 api 19。只有一个构造函数需要 api 19 ,只需使用另一个构造函数。
  • 我用了另一个,现在效果很好。谢谢大家!

标签: java android admob andengine


【解决方案1】:

问题在于 FrameLayout.LayoutParams 构造函数 不支持 另一个 FrameLayout 作为参数,直到 api 19

但是还有另外两个签名:

public FrameLayout.LayoutParams(ViewGroup.LayoutParams 源) - api 1

public FrameLayout.LayoutParams (ViewGroup.MarginLayoutParams 源) - api 1

FrameLayout.LayoutParams 是 ViewGroup.MarginLayoutParams 的子类

所以在检查参数布局是FrameLayout.LayoutParams之后

您可以通过以下方式解决问题:

        if(super.createSurfaceViewLayoutParams() instanceOf FrameLayout.LayoutParams){

         final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
                    new FrameLayout.LayoutParams((ViewGroup.MarginLayoutParams)super.createSurfaceViewLayoutParams());
    }

第一个签名之间的区别 (API 19) 和别的 是新的 LayoutParams 对象不会设置重力

 if(super.createSurfaceViewLayoutParams() instanceOf FrameLayout.LayoutParams){


    ViewGroup.MarginLayoutParams paramLP=(ViewGroup.MarginLayoutParams)super.createSurfaceViewLayoutParams();

    int gravityHolder=((FrameLayout.LayoutParams)super.createSurfaceViewLayoutParams()).gravity;


             final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
                        new FrameLayout.LayoutParams(paramLP);

    surfaceViewLayoutParams.gravity=gravityHolder;
        }

【讨论】:

    【解决方案2】:
    final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
    new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
    

    我的解决方法如下:

    final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
    android.widget.FrameLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,android.view.ViewGroup.LayoutParams.MATCH_PARENT);
    surfaceViewLayoutParams.gravity = Gravity.CENTER;
    

    似乎适用于 Android Studio + Andengine,不需要 API19。

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多