You are on page 1of 22

Phát triển ứng dụng cho

Thiết bị di động
Chapter 5.3. Resources for Adaptive layout
Lưu ý
❖ Các slide trong bài giảng này dựa trên các bài
thực hành của Google Android Labs
❖ Original slides:
https://drive.google.com/drive/folders/1eu-
LXxiHocSktGYpG04PfE9Xmr_pBY5P

2
5.3 Adaptive layouts and
resources

3
Nội dung

● Layout thích ghi và các resource

● Các resource thích nghi

● Các resource mặc định

4
Các layout thích nghi và
resources

This work is licensed under a Creative


Resources for
Android Developer Fundamentals V2 Commons Attribution 4.0 International
adaptive layouts License.
Adaptive layouts là gì?

Layout có thể hiển thị tốt trên các màn


hình có kích thước khác nhau, độ
phân giải khác nhau, hướng khác
nhau

6
Adaptive layout
● Layout thay đổi theo cấu hình
○ Kích thước màn hình
○ Hướng thiết bị
○ Theo địa phương/vùng miền
○ Theo phiên bản của Android

● Cung cấp các resource thích nghi


○ Các strings cục bộ hóa (localized strings)

● Sử dụng các layout tùy biến cao


○ GridLayout

7
Resource folders of a small app
MyProject/
src/
Để tất cả tài nguyên dự
res/
drawable/ án của bạn trong thư
graphic.png mục res
layout/
activity_main.xml
list_iteminfo.xml
mipmap/
ic_launcher_icon.png
values/
strings.xml

8
Các thư mục phổ biến trong res
● drawable/, layout/, menu/

● values/—XML files of simple values, such as


string or color
● xml/—arbitrary XML files

● raw/—arbitrary files in their raw form

● mipmap/—drawables for different launcher icon


densities
● Complete list 9
Alternative resources

10

This work is licensed under a Creative


Resources for
Android Developer Fundamentals V2 Commons Attribution 4.0 International
adaptive layouts License.
Alternative resources là gì?

Cấu hình thiết bị khác nhau có thể sẽ cần các


resource khác nhau
● Các string cục bộ
● Ảnh đa phân giải (nhiều độ phân giải khác nhau)
● Chiều của layout

Android sẽ tự động tải các resources thích hợp một


11
cách tự động
Tạo thư mục alternative resource

Sử dụng các tên thư


mục khác nhau cho
các cấu hình khác
nhau

12
Đặt tên cho các alternative
resource folders
Tên thư mục của resource phải có định dạng sau

drawable-hdpi drawables cho màn hình độ phân giải cao


layout-land layout cho màn hình ngang
layout-v7 layout cho phiên bản khác nhau của
Android
values-fr Layout cho tiếng Pháp

List of directories and qualifiers and usage detail 13


Hướng xoay màn hình

● Sử dụng res/layout để cung cấp layout ngang


nếu cần
○ res/layout-port for portrait-specific layouts
○ res/layout-land for landscape specific layouts

● Tránh hard-coded các tham số về chiều

14
Adaptive layout đơn giản
GridLayout
● In values/integer.xml:
<integer name="grid_column_count">1</integer>
● In values/integer.xml-land:
<integer name="grid_column_count">2</integer>

Landscape Portrait

15
Smallest width
● Smallest-width (sw) trong tên thư mục định
nghĩa chiều rộng nhỏ nhất
○ res/values-swndp, where n is the smallest width
○ Example: res/values-sw600dp/dimens.xml
○ Does not change with orientation

● Android sẽ sử dụng resource gần với (without


exceeding) kích thước chiều rộng nhỏ nhất của
thiết bị
16
Phiên bản của platform
● API level supported by device
○ res/drawables-v14
contains drawables for devices that support API level 14 and above

● Một số resource chỉ hỗ trợ API mới


○ Ví dụ: WebP image format yêu cầu API level 14 trở lên (Android
4.0)

● Android API level


17
Cục bộ hóa
● Provide strings (and other resources) for specific
locales
○ res/values-es/strings.xml

● Increases potential audience for your app


● Locale is based on device's settings
● Localization

18
Default
resources

19

This work is licensed under a Creative


Resources for
Android Developer Fundamentals V2 Commons Attribution 4.0 International
adaptive layouts License.
Default Resources

● Android luôn cung cấp các resource mặc định


○ Tên chung chung (không có chỉ định cụ thể)
○ res/layout, res/values, res/drawables….

● Android sẽ sử dụng resource mặc định nếu


không tìm được local nào trong cấu hình
● Localizing with Resources

20
Tìm hiểu thêm
● Supporting Multiple Screens

● Providing Resource

● Providing Resources Guide

● Resources Overview

● Localization Guide
21
END

22

This work is licensed under a Creative


Resources for
Android Developer Fundamentals V2 Commons Attribution 4.0 International
adaptive layouts License.

You might also like