在android中扩展LinearLayout而不是Activity
我可以有一个扩展 LinearLayout 的类作为 Android 项目的主类吗?我是 Android 开发新手。当我们创建一个新项目时,是否必须扩展Activity类?
如果有可能有一个扩展 LinearLayout 的类,谁能告诉我该怎么做?例如如何将textView、EditText之类的东西添加到LinearLayout中?
Can I have a class which extend a LinearLayout, as the main class of the Android project? I'm new to android developing. Is it a must to extend the Activity class when we creating a new project?
If it is possible to have a class which extend the LinearLayout, can anyone tell me how to do it? For example how to add a textView, EditText something like that to the LinearLayout?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LinearLayout 和 Activity 是 Android 开发中两个截然不同的概念,您应该仔细阅读这两个概念,看看为什么您的问题的答案是“否”。简而言之,布局(包括 LinearLayout)是 UI 构造,是 Activity 中发生的情况的视觉表示。但运行的是 Activity,而不是 Layout。
LinearLayout and Activity are two very different concepts in Android development, and you should read up on both to see why the answer to your question is "no". In short, layouts (including LinearLayout) are UI constructs, which are the visual representation of what's happening in an Activity. But it's the Activity that is run, not the Layout.
是的,创建 Android 项目时,活动是必要的。每个活动本身都通过 setContentView() 托管一个布局。如果可以在单独的类中扩展 LinearLayout,则将其设置为单独活动内的主要内容视图:
然后您可以将布局添加到主布局中:
An activity is necessary, yes, when creating an android project. Each activity itself hosts a layout with setContentView(). If is possible to extend a LinearLayout in a separate class, then set it as the main content view inside of a separate activity:
You can then add the layout to your main layout: