谁能给一个自定义的ViewGroup包含与测量和布局实例自定义视图的一个很好的例子吗? (在Java)自定义、很好、视图、测量

2023-09-04 09:25:21 作者:烟雨江南却痴缠

我一直有几个问题,使用自定义viewgroups和自定义视图。我已经做了很多搜索已经为这一点,但如果我错过了一些随意联系我那里。是的,我读过的一切意见,ViewGroups,测量和布局通过在Android开发网站,但我仍然感到困惑,没有任何Java示例code。

I've been having a few issues with using custom viewgroups and custom views. I've done a lot of searching already for this, but if I missed something feel free to link me there. Yes I have read everything about Views, ViewGroups, measure and layout passes on the android dev website, but I'm still confused without any Java sample code.

1)我的理解是,儿童的意见需要实现onMeasure和onLayout。这是否意味着父母的ViewGroup需要手动调用它的每个儿童的措施()和布局()?如果是的话,应该在哪里这些电话设?他们应该是在父母的onMeasure和onLayout方法?如果是这样,到哪里都是在第一个措施()和布局()启动这整个递归过程调用。

1)My understanding is that child-views need to implement onMeasure and onLayout. Does this mean that the parent ViewGroup needs to manually call measure() and layout() for each of its children? If so, where should these calls be located? Should they be in the Parent's onMeasure and onLayout methods? If so, where are the very first measure() and layout() calls that start this whole recursive process.

2)我看到一个childview布局PARAMS可以使用类似下面的设置。如果这code是父母的ViewGroup的onLayout里面的地方?

2) I see that layout params of a childview can be set using something like the following. Should this code be place inside the onLayout of the parent viewgroup?

CustomView cust = new CustomView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
cust.setLayoutParams(params);

如果我想设置一个MeasureSpec,我只是将其添加为这里的另一个规则?

If I want to set a MeasureSpec, do I just add it as another rule here?

3)它是好创建一个CustomView的实例,它添加到CustomViewGroup里面的$ C $下CustomViewGroup,如果是这样,我应该把这个code在CustomViewGroup构造?如果不是,它只是了解,创建ViewGroups,然后加入孩子们都来自不同的课外?

3) Is it okay to create an instance of a CustomView and add it to CustomViewGroup from inside the code for CustomViewGroup, and if so should I put this code in the CustomViewGroup constructor? If not, is it just understood that you create ViewGroups and then add children all from a different outside class?

4)它是常见的做法来调用平局(),我的每个中的OnDraw()方法中childviews的CustomViewGroup?

4)Is it common to practice to call draw() for each of my childviews inside the onDraw() method for CustomViewGroup?

现在,当我执行一些code画一个圆,它执行的角度绘制方法,但没有实际出现在屏幕上。当我把它添加到一个RelativeLayout的,而不是我customViewGroup,我没有问题。所以我觉得我缺少有关如何获取viewgroups展现出来的关键部分。 (是的,我设置的内容查看到的ViewGroup)。

Right now, when I execute some code to draw a circle, it executes the draw method of view but nothing actually appears on the screen. When I add it to a relativeLayout instead of my customViewGroup, I have no problem...So I think I'm missing a key piece about how to get viewgroups to show up. (Yes, I set the ContentView to the ViewGroup).

在此先感谢!

编辑:请不要告诉我的XML,我做这一切的Java

Please don't show me XML, I'm doing this all in Java.

推荐答案

我建议你阅读这些幻灯片并观看这影片。

I recommend you to read these slides and watch this video.