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);
分享到:
相关推荐
在Android开发中,`LayoutInflater.from(context).inflate()`方法是一个至关重要的组件,用于将XML布局文件转换为视图对象并添加到视图层次结构中。这个方法广泛应用于动态加载和构建用户界面,尤其在处理列表视图、...
例如,在创建自定义对话框或Adapter时,先用 `LayoutInflater.inflate()` 加载XML布局,然后通过 `findViewById()` 查找并操作加载后的视图。这种组合使得我们可以在运行时动态地构建和修改用户界面,增强了应用的...
该方法是LayoutInflater类中的一个成员方法,主要用于将XML布局文件转换成View对象,以便在Android应用程序中使用。 LayoutInflater.inflate()方法的基本用法 LayoutInflater对象的实例可以通过LayoutInflater.from...
1. `int layoutResID`:这是一个整数,表示要加载的XML布局资源的ID。 2. `ViewGroup root`:用于指定父视图,可选。这个视图可以用来作为新创建视图的容器,同时提供合适的LayoutParams。 3. `boolean attachToRoot...
`LayoutInflater.inflate()` 方法是我们经常使用的一个核心方法,尤其是在创建自定义Adapter或动态加载布局时。接下来我们将深入探讨`LayoutInflater.inflate()` 的源码,以理解其工作原理和不同参数版本之间的差异...
XmlPullParser parser = Xml.newPullParser(); parser.setInput(is, null); LayoutInflater inflater = LayoutInflater.from(context); inflater.parseXml(parser); ``` `inflate`的使用对于理解和构建Android用户...
`inflate()` 方法是`LayoutInflater` 的核心功能,用于将XML布局资源动态地加载到活动中。下面将详细解释`LayoutInflater` 及其`inflate()` 方法的工作原理,以及如何在实际应用中灵活使用它们。 `LayoutInflater` ...
总的来说,这个"LayoutInflater inflate 示例demo"是一个很好的学习资源,它将帮助你深入理解Android中布局动态加载的过程,以及如何根据需要有效地使用`LayoutInflater`。通过实践,你将能够熟练掌握这一关键的...
接下来我们将深入探讨如何利用ViewStub来显示和隐藏布局,以及View.VISIBLE和View.GONE这两个属性在控制布局可见性中的作用。 首先,了解ViewStub的基本用法。在XML布局文件中,你可以这样创建一个ViewStub: ```...
2. 加载XML布局:然后,使用`inflate()`方法加载布局文件,如: ```java View view = inflater.inflate(R.layout.dynamic_view, null); ``` 3. 添加到父视图:最后,将新创建的视图添加到目标父视图(例如一个`...
在这个方法中,我们会用到LayoutInflater来加载XML布局文件,该文件定义了Dialog的外观。例如,我们可以创建一个包含“正在加载中...”文本和一个圆形进度条的布局。 ```java public class CustomProgressDialog ...
在代码中,通过调用`ViewStub.inflate()`方法来加载指定的布局: ```java ViewStub viewStub = findViewById(R.id.viewStub); viewStub.inflate(); ``` **二、实现页面状态动态切换** 在Activity中,我们可以...
当调用`inflate()`方法时,LayoutInflater会解析XML布局文件,根据节点信息创建对应的View,并设置相应的属性。这个过程涉及到了`createView()`方法,它会创建一个View对象。为了支持从XML中解析属性,每个自定义...
View rootLayout = inflater.inflate(R.layout.activity_main, null); ``` 三、通过ViewTreeObserver监听 当视图树加载完成时,可以通过ViewTreeObserver的OnGlobalLayoutListener来获取根View。这种方式适用于...
LayoutInflater 的inflate 方法共有四种形式,目的都是把 xml 表述的 layout 转化为 View 对象。其中一个比较常用的是: ```java View inflate(int resource, ViewGroup root) ``` 其中,int resource 是 resource...
4. **利用Inflate布局**:如果你的背景是复杂的XML布局,可以使用LayoutInflater来动态加载一个新的布局,然后将其设置为View的背景。 ```java LayoutInflater inflater = (LayoutInflater) context....
View view = inflater.inflate(R.layout.your_layout, parent, false); ``` 6. JSON与XML: 有时,JSON因其简洁性和易用性,成为替代XML的流行选择。在Android中,可以使用Gson、Jackson或org.json等库进行JSON的...
在Android系统加载XML布局时,会调用LayoutInflater类。LayoutInflater是一个关键组件,它负责解析XML文件并创建对应的View对象。我们通常使用`LayoutInflater.from(Context)`来获取实例,然后调用`inflate()`方法将...