Google Ads MediaView not correctly resizing height to wrap_content when displaying image

后端 未结 5 444
情歌与酒
情歌与酒 2020-12-31 04:54

I got an email from AdMob today saying:

Change to native ads policy: Native ads will require MediaView to render the video or main image asset. In a

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 05:20

    To ensure that all media are as wide as possible and that they respect a maximum height (so there are no surprises depending on the dimensions of the media).

    XML

    
    

    JAVA

    mediaView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
            @Override
            public void onChildViewAdded(View parent, View child) {
                float scale = context.getResources().getDisplayMetrics().density;
                
                int maxHeightPixels = 175;
                int maxHeightDp = (int) (maxHeightPixels * scale + 0.5f);
    
                if (child instanceof ImageView) { //Images
                    ImageView imageView = (ImageView) child;
                    imageView.setAdjustViewBounds(true);
                    imageView.setMaxHeight(maxHeightDp);
    
                } else { //Videos
                    ViewGroup.LayoutParams params = child.getLayoutParams();
                    params.height = maxHeightDp;
                    child.setLayoutParams(params);
                }
            }
    
            @Override
            public void onChildViewRemoved(View parent, View child) {}
        });
    

提交回复
热议问题