最近有个需求,要在react native中嵌入一个原生的视频组件,同事嵌入后发现有问题:
https://musicfe.com/rn-android-webview
参考这个方法,在嵌入之后调用一下。
public void manuallyLayoutChildren(View view) {
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
view.measure(
View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
view.layout(0, 0, width, height);
}
看了一下,是因为内容变化的时候,一是requestLayout没有正常执行;二是上面云音乐给的代码中,没有触发子view的重绘,所以子view的布局失效了。处理方法
@Override
public void requestLayout() {
super.requestLayout();
CustomView.this.post(() -> {
if (ViewCompat.isAttachedToWindow(CustomView.this)) {
layoutViews(CustomView.this);
}
});
}
public void layoutViews(View view) {
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
view.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
view.layout(0, 0, width, height);
view.invalidate();
}
玩~