您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页安卓动态添加控件_向LinearLayout中增加控件

安卓动态添加控件_向LinearLayout中增加控件

来源:二三四教育网

背景:

在有些情况下,需要通过代码自动向页面内增加控件,而不是事先在xml文件中写好。本文介绍将LinearLayout中增加控件。

解决方案:

1.添加单个控件样例

LinearLayout layout = findViewById(R.id.topLayout);
TextView textView = new TextView(this);
textView.setText("测试");
layout.addView(textView);

效果:


2.添加多个控件样例

LinearLayout layout = findViewById(R.id.topLayout);
        for(int i=0;i<5;i++)
        {
            TextView textView = new TextView(this);
            textView.setText("测试"+i);
            layout.addView(textView);
        }

效果:

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务