相对布局中代码添加控件

该博客详细介绍了如何在Android开发中使用DependentLayout来创建并配置文本(Text)和ShapeElement元素,通过设置LayoutConfig实现A、B、C、D、E五个文本元素的相对布局,包括中心、左右、上下关系。
    void addComponentsInDependentLayout(){
        DependentLayout dl = (DependentLayout) findComponentById(ResourceTable.Id_id_dl);

        Text textA = new Text(this);
        int IDA = 1000;
        textA.setId(IDA);
        textA.setText("A");
        textA.setTextAlignment(TextAlignment.CENTER);
        textA.setTextSize(20, Text.TextSizeType.VP);
        ShapeElement shapeElementA = new ShapeElement();
        shapeElementA.setRgbColor(RgbColor.fromArgbInt(0xFF00ff00));
        textA.setBackground(shapeElementA);

        //LayoutConfig(int width, int height)
        DependentLayout.LayoutConfig configA = new DependentLayout.LayoutConfig(200, 200);
        //添加相对布局规则
        configA.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);

        //textA.setMarginRight(20);
        textA.setLayoutConfig(configA);
        dl.addComponent(textA);

        Text textB = new Text(this);
        textB.setText("B");
        textB.setTextAlignment(TextAlignment.CENTER);
        textB.setTextSize(20, Text.TextSizeType.VP);
        ShapeElement shapeElementB = new ShapeElement();
        shapeElementB.setRgbColor(RgbColor.fromArgbInt(0xFF00ff00));
        textB.setBackground(shapeElementB);

        //LayoutConfig(int width, int height)
        DependentLayout.LayoutConfig configB = new DependentLayout.LayoutConfig(200, 200);
        //添加相对布局规则
        configB.addRule(DependentLayout.LayoutConfig.LEFT_OF, IDA);
        configB.addRule(DependentLayout.LayoutConfig.ABOVE, IDA);

        textB.setLayoutConfig(configB);
        dl.addComponent(textB);

        Text textC = new Text(this);
        textC.setText("C");
        textC.setTextAlignment(TextAlignment.CENTER);
        textC.setTextSize(20, Text.TextSizeType.VP);
        ShapeElement shapeElementC = new ShapeElement();
        shapeElementC.setRgbColor(RgbColor.fromArgbInt(0xFF00ff00));
        textC.setBackground(shapeElementC);

        //LayoutConfig(int width, int height)
        DependentLayout.LayoutConfig configC = new DependentLayout.LayoutConfig(200, 200);
        //添加相对布局规则
        configC.addRule(DependentLayout.LayoutConfig.RIGHT_OF, IDA);
        configC.addRule(DependentLayout.LayoutConfig.ABOVE, IDA);

        textC.setLayoutConfig(configC);
        dl.addComponent(textC);

        Text textD = new Text(this);
        textD.setText("D");
        textD.setTextAlignment(TextAlignment.CENTER);
        textD.setTextSize(20, Text.TextSizeType.VP);
        ShapeElement shapeElementD = new ShapeElement();
        shapeElementD.setRgbColor(RgbColor.fromArgbInt(0xFF00ff00));
        textD.setBackground(shapeElementD);

        //LayoutConfig(int width, int height)
        DependentLayout.LayoutConfig configD = new DependentLayout.LayoutConfig(200, 200);
        //添加相对布局规则
        configD.addRule(DependentLayout.LayoutConfig.RIGHT_OF, IDA);
        configD.addRule(DependentLayout.LayoutConfig.BELOW, IDA);

        textD.setLayoutConfig(configD);
        dl.addComponent(textD);

        Text textE = new Text(this);
        textE.setText("E");
        textE.setTextAlignment(TextAlignment.CENTER);
        textE.setTextSize(20, Text.TextSizeType.VP);
        ShapeElement shapeElementE = new ShapeElement();
        shapeElementE.setRgbColor(RgbColor.fromArgbInt(0xFF00ff00));
        textE.setBackground(shapeElementE);

        //LayoutConfig(int width, int height)
        DependentLayout.LayoutConfig configE = new DependentLayout.LayoutConfig(200, 200);
        //添加相对布局规则
        configE.addRule(DependentLayout.LayoutConfig.LEFT_OF, IDA);
        configE.addRule(DependentLayout.LayoutConfig.BELOW, IDA);

        textE.setLayoutConfig(configE);
        dl.addComponent(textE);
    }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值