Android布局中LayoutInflater的使用(利用代码添加xml形式的Layout布局)
2012-07-21 17:24
597 查看
1.什么是LayoutInflater
This class is used to instantiate layout XML file into its corresponding View objects.这个类是代码形式,把xml类型的布局转化成相应的View对象
2.如何获得LayoutInflater(3种方式)
LayoutInflater inflater = LayoutInflater.from(context); LayoutInflater inflater = getLayoutInflater(); LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
3.如何使用LayoutInflater
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); ViewGroup viewGroup = (ViewGroup)this.getLayoutInflater().inflate(R.layout.main, null); SurfaceView view1 = new MySurfaceView(this,Color.RED);//可用其他View代替 SurfaceView view2 = new MySurfaceView(this,Color.BLUE);//可用其他View代替 ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(40, 40); //view2.setZOrderOnTop(true); viewGroup.addView(view1); this.setContentView(viewGroup); //this.addContentView(view2, lp); }
If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.
在Activity中使用,因为R.layout.main为LinearLayout,通过inflate获得的ViewGroup是LinearLayout对象,所以直接addView的话会是默认的layout方式,红色的surfaceView加到了R.layout.main的按钮下方
相关文章推荐
- Android利用dimens.xml进行适配,使用代码生成不同的dimens.xml文件
- android游戏开发框架libgdx的使用(二十二)—利用TABLELAYOUT进行布局
- iOS 8 Auto Layout界面自动布局系列3-使用代码添加布局约束
- android 使用代码实现 RelativeLayout布局
- android使用代码布局LInearLayout达不到xml的效果(自问自答)
- iOS 8 Auto Layout界面自动布局系列3-使用代码添加布局约束
- 黑马程序员————Android中LayoutInflater布局解析的使用
- android xml布局文件中使用RelativeLayout,明明有这个id,却提示找不到---->解决办法
- iOS 8 Auto Layout界面自动布局系列3-使用代码添加布局约束
- android,利用layoutParams代码动态布局空间位置
- 【Android】利用Java代码布局,按钮添加点击事件
- android 使用代码和xml配合进行布局
- android在java代码中动态添加组件及相关布局方法(LayoutParams)
- Android:layout_gravity与gravity的设置与区别(java代码&xml布局文件)
- IOS手动添加的View 在代码中使用(自动布局)autoLayout
- android xml布局文件中使用RelativeLayout,明明有这个id,却提示找不到---->解决办法
- 使用LayoutInflater添加一个布局引用
- Android 利用layoutParams代码动态布局空间位置
- 11_Android中HttpClient的应用,读取网络xml及xml解析流,Handler的应用,LayoutInflater的使用,SmartImageView的使用
- Android利用dimens.xml进行适配,使用代码生成不同的dimens.xml文件