当然,您可以向其中添加尽可能多的按钮和其他小部件
frameLayout。由于
frameLayout允许视图的堆叠,因此您在xml文件中添加的组件现在位于以编程方式添加的视图的后面。以下是如何动态创建和添加小部件的方法:
// find your framelayoutframeLayout = (frameLayout) findViewById(....);// add these after setting up the camera view// create a new ButtonButton button1 = new Button(this);// set button textbutton1.setText("....");// set gravity for text within buttonbutton1.setGravity(Gravity.....);// set button backgroundbutton1.setBackground(getResources().getDrawable(R.drawable.....));// set an onClickListener for the buttonbutton1.setonClickListener(new onClickListener() {....})// declare and initialize LayoutParams for the framelayoutframeLayout.LayoutParams params = new frameLayout.LayoutParams( frameLayout.LayoutParams.WRAP_CONTENT, frameLayout.LayoutParams.WRAP_CONTENT);// decide upon the positioning of the button //// you will likely need to use the screen size to position the// button anywhere other than the four cornersparams.setMargins(.., .., .., ..);// use static constants from the Gravity classparams.gravity = Gravity.CENTER_HORIZONTAL;// add the viewfl1.addView(button2, params);// create and add more widgets........编辑1:
您可以在此处使用一个技巧:
// Let's say you define an imageview in your layout xml file. Find it in pre:imageView1 = (ImageView) findViewById(....);// Now you add your camera view..........// once you add your camera view to the framelayout, the imageview will be // behind the frame. Do the following:framelayout.removeView(imageView1);framelayout.addView(imageView1);// That's it. imageView1 will be on top of the camera view, positioned the way// you defined in xml file
发生这种情况是因为:
子视图是在堆栈中绘制的,最近添加的子视图位于顶部(来自frameLayout的android资源页面)



