是否有可能按钮添加到已设定编程方式的FrameLayout? [英] Is it possible to add buttons to a framelayout that was set programmatically?

查看:207
本文介绍了是否有可能按钮添加到已设定编程方式的FrameLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有点难以说明问题,但我会尽我所能:

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

我开发使用自定义相机活动的Andr​​oid应用程序。在我使用的编程方式创建从表面上看,并将其设置为的FrameLayout这台相机的活动(包括全屏幕),这是在XML布局文件中定义的。

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.

我现在的问题是,我怎么能添加其他元素,框架布局?只有编程?我问,因为到现在为止我只能够以编程方式添加其他元素。我在XML布局添加的元素并没有出现在屏幕上。
难道他们仅仅落后,我加入到框架布局的表面看法?如果是这样,这将有可能使它们向前方

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?

感谢你们!

推荐答案

当然,你可以尽可能多的按钮和其他小部件添加到的FrameLayout 你有。因为的FrameLayout 允许的意见堆放,您在XML文件中添加的组件现在是你编程方式添加视图后面。这里是你如何创建和动态添加小工具:

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

....
....

编辑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

这是因为:

子视图中绘制一摞,顶部最近添加的孩子(从机器人的FrameLayout资源页)

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

这篇关于是否有可能按钮添加到已设定编程方式的FrameLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆