簡體   English   中英

android動態添加布局xml中的元素

[英]android dynamically add element from layout 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);

需要問R.id.mainLayout線性布局是在其他xml文件中還是一樣?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM