将自定义Java视图添加到react-native组件

时间:2019-03-09 14:33:17

标签: java android react-native

我已经在正在运行的应用中实现了这个Applovin react-native示例:https://github.com/AppLovin/Demo-React-Native

我遇到的问题是它在位于某些内容上方的父视图上显示横幅。

我希望能够将横幅放置在各种本机组件中的特定位置。

以下是显示横幅的Java代码:

package com.applovin.apps.demo.reactnative.ad_modules;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.applovin.adview.AppLovinAdView;
import com.applovin.adview.AppLovinAdViewDisplayErrorCode;
import com.applovin.adview.AppLovinAdViewEventListener;
import com.applovin.sdk.AppLovinAd;
import com.applovin.sdk.AppLovinAdClickListener;
import com.applovin.sdk.AppLovinAdDisplayListener;
import com.applovin.sdk.AppLovinAdLoadListener;
import com.applovin.sdk.AppLovinAdSize;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.UiThreadUtil;

public class Banners
    extends AdModule
{
private AppLovinAdView mAdView;

public Banners(ReactApplicationContext reactContext)
{
    super( reactContext );
}

@ReactMethod
public void initAd()
{
    UiThreadUtil.runOnUiThread( new Runnable()
    {
        @Override
        public void run()
        {
            mAdView = new AppLovinAdView( AppLovinAdSize.BANNER, getReactApplicationContext() );

            mAdView.setAdLoadListener( new AppLovinAdLoadListener()
            {
                @Override
                public void adReceived(final AppLovinAd ad)
                {
                    log( "Banner loaded" );
                }

                @Override
                public void failedToReceiveAd(final int errorCode)
                {
                    // Look at AppLovinErrorCodes.java for list of error codes
                    log( "Banner failed to load with error code " + errorCode );
                }
            } );

            mAdView.setAdDisplayListener( new AppLovinAdDisplayListener()
            {
                @Override
                public void adDisplayed(final AppLovinAd ad)
                {
                    log( "Banner Displayed" );
                }

                @Override
                public void adHidden(final AppLovinAd ad)
                {
                    log( "Banner Hidden" );
                }
            } );

            mAdView.setAdClickListener( new AppLovinAdClickListener()
            {
                @Override
                public void adClicked(final AppLovinAd ad)
                {
                    log( "Banner Clicked" );
                }
            } );

            mAdView.setAdViewEventListener( new AppLovinAdViewEventListener()
            {
                @Override
                public void adOpenedFullscreen(final AppLovinAd ad, final AppLovinAdView adView)
                {
                    log( "Banner opened fullscreen" );
                }

                @Override
                public void adClosedFullscreen(final AppLovinAd ad, final AppLovinAdView adView)
                {
                    log( "Banner closed fullscreen" );
                }

                @Override
                public void adLeftApplication(final AppLovinAd ad, final AppLovinAdView adView)
                {
                    log( "Banner left application" );
                }

                @Override
                public void adFailedToDisplay(final AppLovinAd ad, final AppLovinAdView adView, final AppLovinAdViewDisplayErrorCode code)
                {
                    log( "Banner failed to display with error code " + code );
                }
            } );

            View content = getCurrentActivity().findViewById( android.R.id.content );
            if ( content instanceof ViewGroup )
            {
                final FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM );
                ( (ViewGroup) content ).addView( mAdView, layout );
            }

            mAdView.loadNextAd();
        }
    } );
}

@ReactMethod
public void loadNextAd()
{
    mAdView.loadNextAd();
}

@ReactMethod
public void closeAd()
{
    UiThreadUtil.runOnUiThread( new Runnable()
    {
        @Override
        public void run()
        {
            mAdView.removeAllViews();
        }
    } );
}
}

我可以看到这正在控制横幅的附加位置,但是我没有使用Java的经验:

View content = getCurrentActivity().findViewById( android.R.id.content );
        if ( content instanceof ViewGroup )
        {
            final FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM );
            ( (ViewGroup) content ).addView( mAdView, layout );
        }

我希望能够显示设置为特定视图的视图 反应本机组件中的像素尺寸类似于:

<ApplovinBanner
style={{backgroundColor:'#000'}}
/>

任何帮助将不胜感激。

0 个答案:

没有答案