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));

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

也可以利用inflate加载menu
<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.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;
}
}

2.两种刚方法:

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); 

更多相关文章

  1. Android笔记
  2. 2011.07.11(2)——— android 自定义Notification
  3. 安卓巴士Android开发神贴整理
  4. Android实现自定义的 时间日期 控件
  5. Android欢迎界面实现
  6. Android(安卓)JNI学习笔记——so文件动态加载
  7. android中的自定义控件
  8. Android中的classLoader
  9. Android中的ClassLoader

随机推荐

  1. Transparent Activity
  2. Android输入法开发
  3. Android发出HTTP请求,解析返回的JSON
  4. Android应用程序之间共享文字和图片(二)
  5. Caused by: java.lang.NoSuchMethodExcep
  6. Android部分错误以及解决方案汇总
  7. Duplicate files copied in APK META-INF
  8. Android利用TTF文件设置字体
  9. asynchttpclient post方法使用
  10. Android将bitmap保存到自定义路径