To really demonstrate the use of a custom layout, you need to try building something unusual. In the following example, you'll put together a ViewGroup
that arranges its children in a nice circle. It's not a very brilliant layout, nor is it particularly useful, but circles are nice to look at, and it would provide useful negative space in the screen center (which could be filled using a FrameLayout
).
Create a new Java source file in the root package of the project named
CircleLayout.java
, and open it in your editor or IDE.Declare the
CircleLayout
as extending theViewGroup
class:public class CircleLayout extends ViewGroup
Declare the three
ViewGroup
constructors and have them delegate directly to theViewGroup
default constructors:public CircleLayout(Context context) { super(context); } // ...
We'll need to know the largest number of pixels taken up by a child
View
object's width, and the largest number of pixels taken up by a childView
object...