android使用代码动态添加布局

一般来说,做android开发,用xml布局比较多,相对于用代码来布局还是多一些,但是也有一些场景需要用到代码动态添加布局,一些不确定的场景就需要了。

主要代码如下

public class MainActivity extends AppCompatActivity {
    private ConstraintLayout layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initControl();
    }

    private void initView() {
        layout = new ConstraintLayout(this);
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT,ConstraintLayout.LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(layoutParams);
        layout.setBackgroundColor(Color.CYAN);
        this.setContentView(layout);
    }

    private void initControl() {
        // 设置布局并且设置约束
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
        layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
        layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
        layoutParams.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;

        // 添加textview控件
        TextView textView = new TextView(this);
        textView.setText("测试代码动态添加控件!");
        textView.setTextColor(Color.BLACK);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getBaseContext(),((TextView)v).getText().toString(),Toast.LENGTH_LONG).show();
            }
        });
        // 设置布局
        textView.setId((int)8888);
        textView.setLayoutParams(layoutParams);
        layout.addView(textView);

        // 添加第二个控件
        ConstraintLayout.LayoutParams layoutParams1 = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
        layoutParams1.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
        layoutParams1.topToBottom = (int)8888;
                layoutParams1.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;

        TextView textView1 = new TextView(this);
        textView1.setText("测试代码动态添加控件2!");
        textView1.setTextColor(Color.BLACK);
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getBaseContext(),((TextView)v).getText().toString(),Toast.LENGTH_LONG).show();
            }
        });
        textView1.setLayoutParams(layoutParams1);
        layout.addView(textView1);
    }

实现效果如下图所示:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值