android:一个新手 GUI 问题 - 如何在没有布局 XML 文件的情况下声明 viewGroup?

发布于 2024-10-05 20:56:02 字数 578 浏览 2 评论 0原文

在应用程序中,我正在努力解决我有一个自定义视图。 我无法在布局 XML 文件中声明它,因为我将在保存自定义视图实例的活动中使用它,并且我需要访问它(无法覆盖 findViewById...)。

因此我决定将所有 GUI 元素声明到 Activity 中。

但我根本无法向前迈出一步,因为我什至无法实例化 viewGroup...

这就是我正在尝试的:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ViewGroup vg = new ViewGroup(this.getApplicationContext());

    setContentView(vg);

}

我得到“无法实例化 ViewGroup”...

有人可以给出一个直接的例子,说明如何声明一个 viewGroup,它保存视图?

该类的文档也不太适合初学者...所有示例都集中在描述布局 XML 文件中的布局...?

感谢您的努力,举个例子!

In the app, I'm struggling with I have a custom view.
I cannot declare it in layout XML file, because I'm going to use in from the activity that holds my custom view instance and I need to have access to it (cannot override findViewById...).

Thereof I decided to declare all of the GUI elements into the Activity.

But I simply cannot make a single step forward, since I even cannot instantiate viewGroup...

This is what I'm trying:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ViewGroup vg = new ViewGroup(this.getApplicationContext());

    setContentView(vg);

}

and I get 'Cannot instantiate ViewGroup'...

Can someone give a straight-forward example, of how to declare a viewGroup, that holds views?

The documentation of the class is also not very beginner-friendly... all the examples are focused on describing the layout in a layout XML file...?

Appreciate your efford, giving an example!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

财迷小姐 2024-10-12 20:56:02

[ViewGroup][1] 是一个抽象类,您无法实例化它。它定义了一种类,该类将成为在其中放置其他视图的容器。换句话说,像RelativeLayoutLinearLayout这样的布局都是ViewGroup。因此,您可以执行类似的操作:

   @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  LinearLayout vg = new LinearLayout(this);
// set the LayoutParams the way you want.
// and add textviews, imageviews, ... here for instance.
  setContentView(vg);

 }

对于 LayoutParams,我认为你应该从 LayoutParams.Fill_parent 开始

[ViewGroup][1] is an abstract class, you cannot instantiate it. It defines a type of classes that will be container to put other views in them. In other words, layouts like LinearLayout of RelativeLayout are ViewGroup. Thus, you could do something like that :

   @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  LinearLayout vg = new LinearLayout(this);
// set the LayoutParams the way you want.
// and add textviews, imageviews, ... here for instance.
  setContentView(vg);

 }

For the LayoutParams, I think you should start with LayoutParams.Fill_parent

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