我正在尝试实现一个自定义布局,它扩展了RelativeLayout,以便管理其动画内容。动画由TextViews组成,以新闻栏方式滚动,我在RelativeLayout android:clipChildren="false" does not work: Animation will get clipped?中说到了
应用程序启动后,我从网上下载一个新闻标题列表,然后创建TextView并以编程方式添加到此自定义布局。为了开始动画TextViews,我需要它们的位置和尺寸。因此,在将TextViews添加到我的自定义布局后,我需要覆盖一个函数,该函数使用绘制的子TextViews调用并正确设置其尺寸。我试图覆盖onDraw但只调用一次。
这是我的代码:
public class NewsAnimationLayout extends RelativeLayout
{
public NewsAnimationLayout(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
// TODO Auto-generated constructor stub
}
public NewsAnimationLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
setWillNotDraw(false);
// TODO Auto-generated constructor stub
}
public NewsAnimationLayout(Context context) {
super(context);
setWillNotDraw(false);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Here I will animate child objects.
}
};
在这种情况下我该怎么做,你能澄清一下吗?
提前致谢
答案 0 :(得分:0)
看起来我想通了。
覆盖onLayout
可以解决这个问题,只要我们将新子项添加到ViewGroup以便确定其位置和维度,Android系统就会调用它。
NewsAnimationLayout
课程中的此代码报告了正确的位置和尺寸:
@Override
protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed,left,top,right,bottom);
System.out.println("onLayout");
//Here I will animate child objects.
int i;
for(i=0;i<this.getChildCount();i++)
{
View v = this.getChildAt(i);
System.out.println("Child "+i+" X="+v.getX()+" Y="+v.getY());
System.out.println("Child "+i+" W="+v.getWidth()+" H="+v.getHeight());
}
}