java Android 视图添加子项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11554486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android View add child
提问by jkigel
I have the following View and TextView, How can I add the TextView to the View as it's child ?
我有以下 View 和 TextView,如何将 TextView 添加到 View 作为它的子项?
public class MyView extends View {
public MyView(Context context, AttributeSet attrs) {
super(context);
TextView textView = new TextView(context);
textView.setText("Hello My Friends");
}
}
Thanks!
谢谢!
回答by user370305
Instead of View
use ViewGroup
to extend your CustomView
class..
而不是View
使用ViewGroup
扩展您的CustomView
类..
A ViewGroupis a special view that can contain other views (called children.) The view group is the base class for layouts and views containers.
甲的ViewGroup是一个可以包含其它视图的特殊视图(称为孩子)的视图组用于布局和视图容器的基类。
Something Like,
就像是,
public class MyView extends ViewGroup
Now, you can use method called
现在,您可以使用称为
public void addView (View child)
Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.
添加子视图。如果尚未在 child 上设置布局参数,则在 child 上设置此 ViewGroup 的默认参数。
回答by Maxim
You can't add child to view. Only to ViewGroup
您不能添加孩子来查看。仅适用于 ViewGroup