Is it possible to add buttons to a framelayout that was set programmatically?

China☆狼群 提交于 2020-01-11 10:11:36

问题


this is somewhat of of difficult to describe issue but I'll do my very best:

I am developing a an android app that uses a custom camera activity. In this camera activity I use create a surface view programmatically and set it to the framelayout (covers full screen) that was defined in the xml layout file.

My question now is, how can I add other elements to the frame layout? Only programmatically? I am asking because as of now I was only able to add other elements programmatically. Elements that i added in the xml layout didn't appear on screen. Is it possible that they are just behind the surface view that i add to the frame layout? If so, would it be possible to bring them to the front?

Thank you guys!


回答1:


Sure, you can add as many buttons and other widgets to the FrameLayout you have. Since the FrameLayout allows stacking of views, the components that you added in the xml file are now behind the View that you added programmatically. Here's how you can create and add widgets dynamically:

// find your framelayout
frameLayout = (FrameLayout) findViewById(....);

// add these after setting up the camera view        

// create a new Button
Button button1 = new Button(this);

// set button text
button1.setText("....");

// set gravity for text within button
button1.setGravity(Gravity.....);

// set button background
button1.setBackground(getResources().getDrawable(R.drawable.....));

// set an OnClickListener for the button
button1.setOnClickListener(new OnClickListener() {....})

// declare and initialize LayoutParams for the framelayout
FrameLayout.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 corners
params.setMargins(.., .., .., ..);

// use static constants from the Gravity class
params.gravity = Gravity.CENTER_HORIZONTAL;

// add the view
fl1.addView(button2, params);

// create and add more widgets

....
....

Edit 1:

There's a trick you can use here:

// Let's say you define an imageview in your layout xml file. Find it in code:
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

This happens because:

Child views are drawn in a stack, with the most recently added child on top (from android resource page on FrameLayout)




回答2:


http://developer.android.com/reference/android/widget/FrameLayout.html

"FameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute."



来源:https://stackoverflow.com/questions/17931151/is-it-possible-to-add-buttons-to-a-framelayout-that-was-set-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!