在android中扩展LinearLayout而不是Activity

发布于 2024-11-09 15:32:59 字数 183 浏览 0 评论 0原文

我可以有一个扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩起发的微风 2024-11-16 15:32:59

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.

焚却相思 2024-11-16 15:32:59

是的,创建 Android 项目时,活动是必要的。每个活动本身都通过 setContentView() 托管一个布局。如果可以在单独的类中扩展 LinearLayout,则将其设置为单独活动内的主要内容视图:

LinearLayout mainLin  = (LinearLayout)findViewById(R.id.main);
MyLinearLayout m = new MyLinearLayout();

然后您可以将布局添加到主布局中:

setContentView(R.id.main);  // in onCreate()
mainLin.addView(m);

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:

LinearLayout mainLin  = (LinearLayout)findViewById(R.id.main);
MyLinearLayout m = new MyLinearLayout();

You can then add the layout to your main layout:

setContentView(R.id.main);  // in onCreate()
mainLin.addView(m);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文