ViewGroup: make sure all child views have valid LayoutParams

This commit is contained in:
Julian Winkler
2023-09-01 13:16:01 +02:00
parent ceb5df9d39
commit 9ad77d5b62
2 changed files with 20 additions and 9 deletions

View File

@@ -41,11 +41,15 @@ public class ViewGroup extends View implements ViewParent, ViewManager {
} }
public void addView(View child) { public void addView(View child) {
addView(child, -1, null); addView(child, -1);
} }
public void addView(View child, int index) { public void addView(View child, int index) {
addView(child, index, null); LayoutParams params = child.getLayoutParams();
if (params == null) {
params = generateDefaultLayoutParams();
}
addView(child, index, params);
} }
public void addView(View child, LayoutParams params) { public void addView(View child, LayoutParams params) {
@@ -53,20 +57,25 @@ public class ViewGroup extends View implements ViewParent, ViewManager {
} }
public void addView(View child, int width, int height) { public void addView(View child, int width, int height) {
addView(child, new LayoutParams(width, height)); final LayoutParams params = generateDefaultLayoutParams();
params.width = width;
params.height = height;
addView(child, params);
} }
private void addViewInternal(View child, int index, LayoutParams params) { private void addViewInternal(View child, int index, LayoutParams params) {
if (child.parent == this) if (child.parent == this)
return; return;
if (params != null) { if (!checkLayoutParams(params)) {
child.setLayoutParams(params); params = generateLayoutParams(params);
} }
child.setLayoutParams(params);
child.parent = this; child.parent = this;
if (index < 0) if (index < 0)
index = children.size(); index = children.size();
children.add(index, child); children.add(index, child);
native_addView(widget, child.widget, index, params); native_addView(widget, child.widget, index, params);
requestLayout();
} }
public void addView(View child, int index, LayoutParams params) { public void addView(View child, int index, LayoutParams params) {
@@ -312,6 +321,12 @@ public class ViewGroup extends View implements ViewParent, ViewManager {
super(); super();
} }
public MarginLayoutParams(LayoutParams params) {
super();
width = params.width;
height = params.height;
}
public MarginLayoutParams(int width, int height) { public MarginLayoutParams(int width, int height) {
super(width, height); super(width, height);
} }

View File

@@ -27,10 +27,6 @@ public class FrameLayout extends ViewGroup {
@Override @Override
protected native void native_removeView(long widget, long child); protected native void native_removeView(long widget, long child);
public void addView(View child, int index) {
addView(child, index, null);
}
@Override @Override
public LayoutParams generateLayoutParams(AttributeSet attrs) { public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs); return new LayoutParams(getContext(), attrs);