Android: How to use 2 layout

旧时模样 提交于 2019-12-23 04:46:03

问题


I'm trying to do an application where there is a background, and with canvas i draw some lines. But there is a problem. Because to make background and other graphicals things i use a .xml file named "activity_main.xml" and after i do setContentView(R.layout.activity_main); to activate it. But when i draw the line i must use this:

 drawView = new DrawView(this) 
 setContentView(drawView);

Where drawView is the class that allow me to draw the line. So the first setContentView result useless and i don't know how to do some change (for examples the background) if i had to work with drawView!

"You have no way to add a DrawView in your activity_main layout ?" Sorry, i suppose yes, but i don't know how to do.


回答1:


You can add your view to your layout like this:

ViewGroup layout = (ViewGroup) findViewById(R.id.your_layout_id);//your container's id in your activity_main.xml 
drawView = new DrawView(this); 
layout.addView(drawView);



回答2:


Why not just add the DrawView to your existing layout with addView()? You'll have to position it correctly of course, probably within a RelativeLayout.



来源:https://stackoverflow.com/questions/20274104/android-how-to-use-2-layout

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