android从布局xml动态添加元素

杰克逊克尔:

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button android:layout_width="wrap_content" android:text="Button" android:layout_height="wrap_content" android:id="@+id/myButton"></Button>
</LinearLayout>

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

**解决方案(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);
香气:
View view = LayoutInflater.from(context).inflate(R.layout.my_layout);
Button button = (Button) view.findViewById(R.id.myButton);
....
aGroupView.addView(button);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章