本文介绍了安卓查看添加子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的查看和TextView的,我怎么能TextView的添加到视图,因为它的孩子?
I have the following View and TextView, How can I add the TextView to the View as it's child ?
public class MyView extends View {
public MyView(Context context, AttributeSet attrs) {
super(context);
TextView textView = new TextView(context);
textView.setText("Hello My Friends");
}
}
谢谢!
推荐答案
而不是查看
使用的ViewGroup
延长你的 CustomView
类..
Instead of View
use ViewGroup
to extend your CustomView
class..
一个的ViewGroup 是一种特殊的视图,可以包含其他视图(称为儿童)的视图组是布局和视图的容器基类。
A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers.
像,
public class MyView extends ViewGroup
现在,你可以使用的方法称为
Now, you can use method called
public void addView (View child)
添加一个子视图。如果没有布局参数对孩子已经设置,默认参数的ViewGroup这是对孩子进行设置。
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.
这篇关于安卓查看添加子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!