Chuyển đến nội dung
Skillpedia
Ứng dụng di động10k · #11918

adaptive

Skill này làm gì

Skill này hướng dẫn làm cho ứng dụng Compose Android phản ứng với nhiều kích thước màn hình và thiết bị khác nhau. Nó bao quát điều hướng, bố cục nhiều khung, danh sách và việc giữ trạng thái khi xoay màn hình. Nó dành cho lập trình viên Android muốn ứng dụng hoạt động vượt ra ngoài điện thoại.

Khi nào nên dùng

  • ✓Khi bạn muốn ứng dụng Android hoạt động tốt trên máy tính bảng và màn hình gập cũng như điện thoại.
  • ✓Khi bạn cần bố cục nhiều khung hoặc thanh điều hướng thay đổi theo kích thước màn hình.
  • ✓Khi bạn muốn các màn hình và việc phát media của ứng dụng giữ được trạng thái qua các thay đổi cấu hình.

Liên kết skill

https://github.com/android/skills/blob/main/jetpack-compose/adaptive/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/android/skills/blob/main/jetpack-compose/adaptive/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm cho ứng dụng Compose Android của tôi thích ứng với máy tính bảng, màn hình gập và cửa sổ máy tính.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Skill bắt đầu bằng việc đảm bảo đã có các kiểm thử ảnh chụp màn hình cho các kích thước màn hình chính, rồi làm cho điều hướng, bố cục và danh sách thích ứng từng bước. Nó cũng xử lý việc giữ trạng thái và hỗ trợ màn hình gập.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Prerequisites

The app must:

  • Use Compose for all screens. If it's still using Fragments or Views, suggest using the XML to Compose skill to migrate those screens.
  • Use Jetpack Navigation 3. If it doesn't, suggest the Navigation 3 skill to migrate the app.

Workflow to make an app adaptive

To make an app adaptive, follow these steps or a subset of them adapting to the task.

  • Step 1: Verify current UI
  • Step 2: Make the navigation bar adaptive
  • Step 3: Add multi-pane layouts
  • Step 4: Make vertical lists adaptive by changing the number of columns
  • Step 5: Hide app bars when scrolling
  • Step 6: Preserve UI state across configuration changes
  • Step 7: Add desktop experiences
  • Step 8: Support foldable devices

Step 1. Verify current UI

Ensure that screenshot tests exist to verify the current UI on different form factors. If they don't exist, add the Compose Preview Screenshot Testing tool. Use the following annotation to create previews for all the major form factors. For example:

@Preview(name = "Phone", device = Devices.PHONE, showBackground = true)
@Preview(name = "Foldable", device = Devices.FOLDABLE, showBackground = true)
@Preview(name = "Tablet", device = Devices.TABLET, showBackground = true)
@Preview(name = "Desktop", device = Devices.DESKTOP, showBackground = true)
annotation class FormFactorPreviews

@PreviewTest
@FormFactorPreviews
@Composable
fun FeedScreenPreview() {
    SnippetsTheme {
        Box {
            Text("My Screen")
        }
    }
}
<br />

Step 2. Make the navigation bar adaptive

Bottom navigation bars are optimized for touch input when the user is holding a phone in portrait mode. On larger screen hand-held devices, like tablets and unfolded foldables, the navigation area must be accessible from the edge of the screen (navigation rail).

If you need to provide more screen space for the content, hide the navigation area. Examples of this include:

  • Hiding the navigation bar when the user scrolls down and showing it again when the user scrolls up. The assumption is that when the user is scrolling down, they are consuming content but when scrolling up they are trying to navigate away from that content.
  • Hiding the navigation area when its content is distracting. For example, in camera previews or when displaying a full-screen photo.

When the detail screen is displayed full-screen on mobile, full-screen mode must be deactivated on larger screens.

Steps to migrate:

  • Locate the existing navigation bar.
  • Convert each item to a NavigationSuiteItem.
  • Identify whether the navigation bar's visibility changes. For example, if it is wrapped with an AnimatedContent or AnimatedVisibility composable. If so, follow the guidance in the "Control navigation area visibility".
  • Replace the container that held the navigation bar (often a Scaffold) with NavigationSuiteScaffold from the Material 3 adaptive layouts library.
  • Supply the navigation items using the navigationItems parameter of NavigationSuiteScaffold.

Step 2.1. Control navigation area visibility

If the navigation bar's visibility changes - it is hidden under certain scenarios or on certain screens - this behavior must be maintained with the adaptive navigation area. This is done using NavigationSuiteScaffold's state parameter.

Steps to migrate:

  • Identify the scenarios under which the navigation bar is hidden. This is usually done with a boolean variable for the visibility. Use isNavBarVisible or shouldShowNavBar as the variable name.
  • Create an instance of NavigationSuiteScaffoldState using rememberNavigationSuiteScaffoldState() and pass it to NavigationSuiteScaffold.
  • When the navigation area visibility changes, use a LaunchedEffect to call show or hide on the NavigationSuiteScaffoldState.

For example:

// Pass this variable to any composable that needs to control the navigation area visibility
var isNavBarVisible by remember { mutableStateOf(true) }
val scaffoldVisibilityState = rememberNavigationSuiteScaffoldState()

NavigationSuiteScaffold(
    navigationSuiteItems = navItems,
    state = scaffoldVisibilityState
) {
    // Main content
}

LaunchedEffect(isNavBarVisible){
    if (isNavBarVisible) {
        scaffoldVisibilityState.show()
    } else {
        scaffoldVisibilityState.hide()
    }
}
<br />

Step 3. Add multi-pane layouts using Navigation 3

Câu hỏi thường gặp

Ứng dụng phải đã dùng gì?

Ứng dụng phải dùng Jetpack Compose cho các màn hình và Jetpack Navigation 3. Nếu chưa, skill gợi ý chuyển đổi trước.

Nó có thêm kiểm thử không?

Có. Nó đảm bảo có các kiểm thử ảnh chụp màn hình để kiểm tra giao diện hiện tại trên các dạng thiết bị khác nhau.

Nó có hỗ trợ chuột và bàn phím không?

Có. Skill bao quát các thiết bị trỏ như chuột và nhập văn bản bằng bàn phím.

Chi tiết

Mô tả gốc của tác giả

Instructions to make or update an app's UI so that it adapts to different Android devices including phones, tablets, foldables, laptops, desktop, TV, Auto, and XR. It includes how to handle different window sizes, pointing devices (such as mouse) and text entry devices (such as keyboard) using the Compose MediaQuery API, and how to preserve UI, navigation, scroll, and media playback state across configuration changes (such as folding/unfolding a foldable, rotating a device, or resizing a window). It also covers multi-pane layouts using Navigation3 Scenes, adaptive UI components (such as button

repo: android/skills

Độ phổ biến

Sao GitHub
10k
Vị trí xếp hạng
#11918
Giấy phép
Apache-2.0
Cập nhật
29/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự