初学者要知道的自定义控件步骤

public class RelativLayout extends ViewGroup {
int groupHeight, groupWidth;


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
groupHeight = h;
groupWidth = w;
}


public RelativLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}


// 测量每个子控件
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);


// 得到子控件个数
int childCount = this.getChildCount();
for (int i = 0; i < childCount; i++) {
// 得到一个子控件
View childView = this.getChildAt(i);
// 测量子控件
childView.measure(widthMeasureSpec, heightMeasureSpec);
}


}


// 指定子控件如何显示
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 找到第一个子控件
View view1 = this.getChildAt(0);
View view2 = this.getChildAt(1);
// 模拟出RelativLayout  指定相对布局的位置
view1.layout(0, 0, ?, ?);
view2.layout(?, ?, ?, ?);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值