android:一个新手 GUI 问题 - 如何在没有布局 XML 文件的情况下声明 viewGroup?
在应用程序中,我正在努力解决我有一个自定义视图。 我无法在布局 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[ViewGroup][1]
是一个抽象类,您无法实例化它。它定义了一种类,该类将成为在其中放置其他视图的容器。换句话说,像RelativeLayout
的LinearLayout
这样的布局都是ViewGroup。因此,您可以执行类似的操作:对于
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 likeLinearLayout
ofRelativeLayout
are ViewGroup. Thus, you could do something like that :For the
LayoutParams
, I think you should start with LayoutParams.Fill_parent