您的位置:首页 > 移动开发 > Android开发

Android 中LayoutInflater的使用

2011-03-24 16:54 447 查看
LayoutInflater的使用,在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),不同点是:

LayoutInflater是用来找layout下xml布局文件,并且实例化!

findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。

为了让大家容易理解我做了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。

效果图如下:

.placeslist_linearlayout.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<LinearLayout android:id="@+id/placeslist_linearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

</LinearLayout>
</ScrollView>


[/b]

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.placeslist_linearlayout);

linearLayout.addView(place_type_text);

这是可运行的,这上面的xml中,LinearLayout不再是Layout的代表,而只是一个普通的View。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: