viewgroup用addview添加的view不显示问题

先看代码:

public class MyviewGroup extends ViewGroup {
private final Context context;
private View view;

public MyviewGroup(Context context) {
super(context,null);
this.context = context;
}

public MyviewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
removeAllViews();
this.setFocusable(false);
this.setClickable(false);
this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
initview();
}

private void initview() {
if (null == view){
view = LayoutInflater.from(context).inflate(R.layout.itme, null, true);
}

addView(view);
LinearLayout.LayoutParams params =
new android.widget.LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.width = 200;
params.height = 200;
view.setLayoutParams(params);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
Log.e("tag", "onLayout: "+childCount );
int top = 0;
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);

view.layout(l+top,t,top+l+60,t+60);
top = top+70;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int count = getChildCount();
for (int i = 0; i < count; i++) {
//这个很重要,没有就不显示
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
}
}

addview之后要重新给子view设置宽高。


转载于:https://www.cnblogs.com/renjiemei1225/p/6215671.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值