java-是否可以将按钮添加到以编程方式设置的框架布局中?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-是否可以将按钮添加到以编程方式设置的框架布局中?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2301字,纯文字阅读大概需要4分钟。
内容图文
![java-是否可以将按钮添加到以编程方式设置的框架布局中?](/upload/InfoBanner/zyjiaocheng/692/f75fb7f7029143ddbb85ce22224b1844.jpg)
这有点难以描述,但我会尽力而为:
我正在开发一个使用自定义摄像头活动的android应用.在此摄影机活动中,我使用编程方式创建一个表面视图,并将其设置为xml布局文件中定义的框架布局(全屏显示).
我现在的问题是,如何在框架布局中添加其他元素?仅以编程方式?我问是因为到目前为止,我只能以编程方式添加其他元素.我在xml布局中添加的元素没有出现在屏幕上.
它们是否可能位于我添加到框架布局的表面视图的后面?如果是这样,是否有可能将它们带到最前面?
感谢大伙们!
解决方法:
当然,您可以在FrameLayout中添加尽可能多的按钮和其他小部件.由于FrameLayout允许堆叠视图,因此您在xml文件中添加的组件现在位于以编程方式添加的视图的后面.以下是如何动态创建和添加小部件的方法:
// 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:
您可以在此处使用一个技巧:
// 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
发生这种情况是因为:
Child views are drawn in a stack, with the most recently added child on top (from android resource page on FrameLayout)
内容总结
以上是互联网集市为您收集整理的java-是否可以将按钮添加到以编程方式设置的框架布局中?全部内容,希望文章能够帮你解决java-是否可以将按钮添加到以编程方式设置的框架布局中?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。