关闭

android布局的学习(VG)

标签: androidbuttonclass
320人阅读 评论(0) 收藏 举报
分类:
public class MyViewGroup extends ViewGroup {  
    public MyViewGroup(Context context) {  
        super(context);  
        this.initOtherComponent(context);  
    }  
    private void initOtherComponent(Context context) {  
        Button aBtn = new Button(context);  
        // set id 1  
        aBtn.setId(1);  
        aBtn.setText("a btn");  
        this.addView(aBtn);  
        Button bBtn = new Button(context);  
        // set id 2  
        bBtn.setId(2);  
        bBtn.setText("b btn");  
        this.addView(bBtn);  
    }  
    @Override  
    protected void onLayout(boolean changed, int l, int t, int r, int b) {  
        int childCount = getChildCount();  
        for (int i = 0; i < childCount; i++) {  
            View child = getChildAt(i);  
            switch (child.getId()) {  
            case 1:  
                // 1 is aBtn  
                Log.d("MyViewGroup", "btn1 setting"); 
                Log.d("MyViewGroup", "r="+r+":l"+l+":t"+t+":b"+b);  
                child.setVisibility(View.VISIBLE);  
                child.measure(r - l, b - t);//!!!!!
                child.layout(0, 0, child.getMeasuredWidth(), child  
                        .getMeasuredHeight());  
                break;  
            case 2:  
                // 2 is bBtn  
                Log.d("MyViewGroup", "btn2 setting");  
                child.setVisibility(View.VISIBLE);  
                child.measure(r - l, b - t);  
                child.layout(0, 50, child.getMeasuredWidth(), child.getMeasuredHeight() + 50);  
                break;  
            default:  
            }  
        }  
    }  
}  

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:63330次
    • 积分:995
    • 等级:
    • 排名:千里之外
    • 原创:37篇
    • 转载:0篇
    • 译文:0篇
    • 评论:11条
    文章分类
    最新评论