求教Android,动态添加到控件能动态删除吗?

求教Android,动态添加到控件能动态删除吗?,第1张

可以的,android中使用布局是为了加快开发,最终控件还是通过解析XML后,通过代码添加的。

具体方法:

例如你的布局是一个Linearlayout linear上面有一本Button btn1

要删除这个btn1要做的就是 linear.removeView( btn1 )

动态添加也是一样的:

linear.addView( btn2 )

要注意的是,Button btn2 = new Button( context )这时候控件是没有大小的,必须设置控件大小以后添加了才能看到。设置控件大小的方法是view.setLayoutParams()

希望能够帮到你。

jquery文档 *** 作的append()或 appendTo()方法可以在被选元素的结尾插入内容,因此可用于动态创建select控件

$("div").append("<select></select>") // 在已经存在的div标签下创建select元素

$("<option></option>").appendTo($("select")) // 为select元素添加option选项

实例演示:动态创建一个含有两个选项——“黑”“白”的select控件,并默认选中“白”这个选项

示例代码如下

创建Html元素

<div class="box">

<span>点击按钮后创建select标签,并默认选中“白”这个选项:</span><br>

<div class="content"></div>

<input type="button" value="创建select标签">

</div>

设置css样式

div.box{width:300pxpadding:20pxmargin:20pxborder:4px dashed #ccc}

div.box span{color:#999font-style:italic}

div.content{width:250pxmargin:10px 0padding:20pxborder:2px solid #ff6666}

select{width:150pxheight:30pxborder:1px solid #6699FF}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11642107.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存