onMeasure 在我的自定义视图组 android 中没有被调用
我有两个自定义 viewgroups
,superViewGroup
和 subViewGroup
。子视图组包含视图。我将我的 superviewgroup 添加到 LinearLayout,并将 subViewGroups
添加到我的 superviewgroup
。
超级视图组 onMeasure()
被调用,但不在子视图组中。但在这两种情况下,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());
}
}
}
}
欢迎评论。提前致谢。
Im having two custom viewgroups
, superViewGroup
and subViewGroup
. The subviewgroup contains views. Im adding my superviewgroup to a linearLayout and the subViewGroups
to my superviewgroup
.
The superviewgroup onMeasure()
is getting called but not in the subviewgroup. but in both cases onLayout()
method is getting called.
The code as follows
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());
}
}
}
}
Comments are appreciated. thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为你必须实际将测量传递给孩子们的观点:
否则你永远不会真正测量你的孩子。由您决定如何执行此操作。仅仅因为您的
SuperViewGroup
处于线性布局,您的SuperViewGroup
就负责测量其子级。Because you have to actually pass the measure to the children views:
Otherwise you never actually measure your children. It is up to you to decide how to do this. Just because your
SuperViewGroup
is in a linear layout, yourSuperViewGroup
takes on responsibility to measure its children.