栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

是否可以将按钮添加到以编程方式设置的框架布局中?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

是否可以将按钮添加到以编程方式设置的框架布局中?

当然,您可以向其中添加尽可能多的按钮和其他小部件

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资源页面)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455164.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号