2.3.9 Nested Views Codehs 〈1080p 2027〉
But fear not. This article will break down exactly what "nested views" means, why the concept is crucial for real-world UI/UX design, and how to ace the 2.3.9 exercise step-by-step. In the context of CodeHS (which often uses a library similar to graphics.js or tab.js for mobile/tablet app design), a view is a rectangular container that holds graphical elements or other views. When we say "nested," we mean one view is placed inside another.
function start() { // 1. Parent View (the main container) var parent = new Rectangle(300, 400); parent.setPosition(100, 100); parent.setColor("#E0E0E0"); // Light gray parent.setBorderWidth(2); parent.setBorderColor("black"); add(parent); // 2. Nested Child 1: Header Bar var header = new Rectangle(260, 50); header.setColor("#4A90E2"); // Blue header.setPosition(parent.getX() + 20, parent.getY() + 20); add(header); 2.3.9 nested views codehs
var parentView = new Rectangle(300, 400); parentView.setPosition(50, 50); // Position on the canvas parentView.setColor("lightgray"); parentView.setBorderWidth(2); add(parentView); Now, create a child that sits inside the parent. The key is that its x and y are relative to the parent’s position . If the parent is at (50, 50), and you want the child at the top-left corner of the parent, you set the child’s position to (50, 50) on the canvas, OR you set it relative to the parent. But fear not
var contentView = new Rectangle(260, 300); contentView.setColor("white"); contentView.setBorderWidth(1); contentView.setPosition(parentView.getX() + 20, parentView.getY() + 80); add(contentView); Place a button or a text block inside contentView . When we say "nested," we mean one view
// 3. Text nested inside Header var headerText = new Text("My App"); headerText.setColor("white"); headerText.setPosition(header.getX() + 15, header.getY() + 32); headerText.setFont("18pt Arial"); add(headerText);