我有两个自定义的viewgroups
,superViewGroup
和subViewGroup
。子视图组包含视图。我正在将我的SuperviewGroup添加到LinearLayout,并将subViewGroups
添加到我的superviewgroup
。
正在调用SuperviewGrouponMeasure()
,但不在子ViewGroup中。但在这两种情况下都会调用onLayout()
方法。
代码如下
public class SuperViewGroup extends ViewGroup{
public SuperViewGroup(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i("boxchart","INSIDE ON MEASURE SUPER VIEWGROUP");
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
child.layout(0, 0, getWidth(), getHeight());
}
}
}
}
public class SubViewGroup extends ViewGroup{
public SubViewGroup(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i("boxchart","INSIDE ON MEASURE SUB VIEWGROUP");
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
child.layout(0, 0, getWidth(), getHeight());
}
}
}
}
欢迎评论。提前谢谢。
最佳答案
因为您必须将度量值实际传递给子视图:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i("boxchart","INSIDE ON MEASURE SUPER VIEWGROUP");
final int count = getChildCount();
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
//Make or work out measurements for children here (MeasureSpec.make...)
measureChild (child, widthMeasureSpec, heightMeasureSpec)
}
}
}
否则你永远不会真正测量你的孩子。如何做到这一点取决于你自己。就因为你的
SuperViewGroup
是线性布局,你的SuperViewGroup
就有责任测量它的孩子。