锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

android从布局xml动态添加元素

时间:2022-12-10 21:30:01 thx03微量程动态扭矩传感器

如何从此布局xml中获取元素myButton

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

并将其放在我选择的布局中(通过代码)?

解决方案(thx aromero)

LinearLayout view = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.my_button, null);

// or LinearLayout buttonView = (LinearLayout)this.getLayoutInflater().inflate(R.layout.my_button, null);

Button myButton = (Button) view.findViewById(R.id.myButton);

view.removeView(myButton);

LinearLayout mainView = (LinearLayout)this.findViewById(R.id.mainLayout);

mainView.addView(myButton);

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章