使用 FrameLayout 将 Activity 扩展到多个活动并重用代码
我不确定这是否是正确的方法。我有一个在frame.xml 中使用FrameLayout
的ChatActivity
。该活动需要在 5 个活动中重复使用。有没有办法进行代码重用?此活动独立于其他活动运行。
例如,在使用 main.xml 的活动 A 中,我希望包含 ChatActivity
和frame.xml。除了将ChatActivity 和frame.xml 合并到活动A 和main.xml 之外,实现此目的的最佳方法是什么?合并活动意味着我必须将代码复制并粘贴到不同的活动中 5 次。我不确定这是否是正确的方法......
I am not sure if this is the right way to do it. I have a ChatActivity
using FrameLayout
in frame.xml. This activity needs to be reused across 5 activities. Is there anyway to do a code reuse? This acitivty runs independently of other activities.
For example, in activity A, which uses main.xml, I want ChatActivity
and frame.xml to be included. What is the best way to achieve this besides merging the ChatActivity
and frame.xml into activity A and main.xml? Merging the activities would mean that I have to copy and paste codes 5 times into different activities. I am not sure if this is the right way...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能将一个
Activity
“包含”到另一个Activity
。由于您的Activity
具有所有其他Activity
使用的基本功能,因此您可以让所有Activity
扩展此基本Activity.
不过,对您来说最好的方法是使用
Fragments
和兼容性库。关于布局,您可以拥有可重用的布局,并使用
include
将它们导入到当前布局中。希望这有帮助!
You cannot "include" an
Activity
to anotherActivity
. Since yourActivity
has a basic functionality that all your otherActivities
use, you could have all yourActivities
extend this basicActivity
.The best way though for you would be to use
Fragments
and the compatibility library.Regarding layouts, you can have reusable ones and import them to your current layout using
include
.Hope this helps!
使用碎片。您可以创建一个片段并在所有活动中使用相同的片段。碎片有自己的看法。查看以下链接的片段。
http://developer.android.com/guide/topics/fundamentals/fragments.html
您可以通过包含兼容性库来使用3.0之前的片段。
http://android-developers.blogspot.com/2011/03 /fragments-for-all.html
Use Fragments. you can create one Fragment and use the same in all activities. Fragments have their own view. Look at the following link for fragment.
http://developer.android.com/guide/topics/fundamentals/fragments.html
you can use fragments before 3.0 by including compatibility library.
http://android-developers.blogspot.com/2011/03/fragments-for-all.html
在 android 中,您可以通过使用 include 标签来重用 xml 文件,例如
要与其他活动共享活动的功能,创建具有通用功能的基本活动,并使其他活动从其扩展。
I android you can reuse xml files by using the include tag like
To share functionalites of an activity with other activities, create a base activity with common functionalites and make the other activities extend from it.