[πHarmonyOS Development Treasure Cases Revealed! So many great resources hidden in the official docs!
Hello everyone~ Recently, while tinkering with HarmonyOS app development, I accidentally discovered a bunch of super practical development cases hidden in the official documentation! Especially the "One-to-Many Development" example for long video appsβafter reading it, I was amazed: "So it can be done like this!" Today, let's dig into these hidden treasures together, with step-by-step code analysis!
π₯ Long Video App Case: One Development for Four Types of Devices
Core Features: Home waterfall recommendations, smart search, watch and comment, full-screen playback
Supported Devices: Mobile/Foldable/Tablet/PC all supported!
π οΈ Multi-Device Adaptation Tricks
1. Dynamic Grid Layout on Home Page
- Mobile Portrait: Two-column video stream
- Tablet Landscape: Three columns + immersive banner
- PC Large Screen: Side navigation + 4K video preview
// Dynamic grid listening code snippet
GridRow({ columns: { sm: 4, md: 8, lg: 12 }}) {
GridCol({ span: { sm: 4, md: 4, lg: 3 }}) {
VideoCard().aspectRatio(16/9) // Adaptive aspect ratio
}
}
2. Foldable Screen Magic
- Half-fold hover playback
- Auto switch to split-screen mode when unfolded
// Fold crease region detection
display.getCurrentFoldCreaseRegion().then(region => {
if(region.creaseRects.length > 0) {
this.avoidArea = region.creaseRects[0]
}
})
π‘ Must-Learn Core Tech Points
-
ArkUI Reactive Three Moves
- Breakpoint listening:
<font style="color:rgba(0, 0, 0, 0.9);background-color:rgb(252, 252, 252);">@ohos.mediaquery</font>
- Flexible layout:
<font style="color:rgba(0, 0, 0, 0.9);background-color:rgb(252, 252, 252);">Flex+Percentage</font>
- Component reuse: HAR package cross-project call
- Breakpoint listening:
- Project Management Tips
project/
ββ common/ # Common utility package
ββ features/ # Feature modules
β ββ home/ # Home HAR
β ββ player/ # Player HAR
ββ products/ # Device-specific adaptation
ββ phone/ # Mobile enhancements
ββ pc/ # Keyboard shortcuts
π― More Practical Case Recommendations
Case 1: E-commerce App (Double 11 Special)
- Mobile: Vertical product waterfall
- Tablet: Left category navigation + right products
- PC: Three-column layout (category/product/detail on the same screen)
// Product card adaptation
@Component
struct GoodsCard {
@Prop isPC: boolean = false
build() {
Column() {
Image($r('app.media.product')).height(this.isPC ? 200 : 120)
Text('Product Name').fontSize(this.isPC ? 18 : 14)
}
}
}
Case 2: News App (Free Switch Between Portrait and Landscape)
- Portrait: Title list + thumbnail
- Landscape: Left navigation + right mixed text and images
- Keyboard adaptation: Arrow keys to switch focus, Enter to open details
π Pitfall Guide: Common Issues in Multi-Device Development
- Image Distortion: β Wrong approach: Fixed width and height β Correct solution: `.aspectRatio()+objectFit**
plain
Image($r('app.media.banner'))
.aspectRatio(16/9)
.objectFit(ImageFit.Cover)
-
Interaction Conflicts:
- Disable mouse hover effects on mobile
- Add keyboard shortcuts on PC
`plain
@State hoverEffect: boolean = false
Button('Buy Now')
.onHover(() => {
if(!isMobile){
this.hoverEffect = !this.hoverEffect
}
})
`
π Learning Shortcuts:
- IDE built-in template: Check "Multi-device Template" when creating a new project
- Debugging tool:
<font style="color:rgba(0, 0, 0, 0.9);background-color:rgb(252, 252, 252);">Previewer</font>
Real-time device switching - Official community: Live Q&A every Thursday at 8pm (search "HarmonyOS Night Talk")
After reading, do you feel that "One-to-Many Development" in HarmonyOS is not that mysterious? In fact, as long as you master adaptive layout + modular design, conquering multiple devices with one set of code is really easy! What other tricky adaptation problems have you encountered in development? Feel free to discuss in the comments~
Top comments (0)