首页 > 代码库 > 动态添加组件(XML)

动态添加组件(XML)

1.利用LayoutInflater的inflate动态加载XML
mLinearLayout = (LinearLayout)findViewById(R.id.LinearLayout_ID);
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(resource--需要加载的XML, null);
XML:resource = R.layout.XML-Name;
mLinearLayout.removeAllViewsInLayout();移除当前LinearLayout的内容
mLinearLayout.addView(view);添加XML到LinearLayout

设置Width和Hight:
   LinearLayout.LayoutParams.FILL_PARENT、mLinearLayout.getLayoutParams().width等
另:
mLinearLayout.addView(v,
   new LinearLayout.LayoutParams(mLinearLayout.getLayoutParams().width,
   mLinearLayout.getLayoutParams().height));

2.利用View.inflate加载xml
Layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
     xmlns:android="
http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
     android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<TextView android:id="@+id/box_0"
   android:layout_width="fill_parent" android:layout_height="40px"
   android:background="#ff00ff00" android:text="This is main layout" />
<LinearLayout
   android:id="@+id/box_1" android:layout_width="fill_parent"
   android:layout_height="wrap_content" android:layout_below="@+id/box_0">
</LinearLayout>
</RelativeLayout>

Layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
android:id="@+id/box" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent" >

<TextView android:id="@+id/box_3" android:layout_width="fill_parent"
   android:layout_height="40px" android:background="#ffff0000"
   android:text="this is 2" />
</LinearLayout>

    setContentView(R.layout.layout_1);
    LinearLayout ll = (LinearLayout) findViewById(R.id.box_1);
    View vv = View.inflate(this, R.layout.layout_2, null);
    ll.addView(vv,      new LinearLayout.LayoutParams   ll.getLayoutParams ().width,ll.getLayoutParams().height));

这个主要是在某一个控件里面动态加载。

3.也可以利用inflate加载menu 首先在res/menu下创建自己的Menu XML—my_menu.xml:
<menu xmlns:android="
http://schemas.android.com/apk/res/android">
<item android:id="@+id/home"
    android:title="Home"
    android:icon="@drawable/home" >
</item>
<item android:id="@+id/help" android:title="Help"
    android:icon="@drawable/help" >
</item>
<item android:id="@+id/save" android:title="Save n Quit"
    android:icon="@drawable/save_quit" >
</item>
<item android:id="@+id/exit" android:title="Quit"
    android:icon="@drawable/exit" >
</item>
</menu>

public class Welcome extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return true;
}

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
     case R.id.home:
     // TODO
     break;
      case R.id.help:
      // TODO
     break;
     case R.id.save:
     // TODO
    break;
     case R.id.exit:
     // TODO
    break;
   }
   return true;
   }
}

4.两种方法:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_sms,
   (ViewGroup) findViewById(R.id.toast_sms_root));


LayoutInflater mInflater = LayoutInflater.from(context);
View myView = mInflater.inflate(R.layout.customToast, null);
TextView sender = (TextView) myView.findViewById(R.id.sender);
TextView message = (TextView) myView.findViewById(R.id.message);

动态添加组件(XML)