Giới thiệu
Resource được hiểu là các hình ảnh, âm thanh, text cứng (đặt trong các thư mục của project) được sử dụng trong project của ứng dụng. Ví dụ như icon, tên của ứng dụng…
Khi tạo một project Android các bạn thấy rằng có một thư mục res (res là viết tắt cả Resource) được tạo ra, và trong thư mục này có khá nhiều thư mục khác nữa chứa các loại resource khác nhau như drawable, color, dimen, string, bool, integer…Và để hiểu rõ hơn Resource trong Android các bạn cùng mình khám phá qua bài viết dưới đây.
Cấu trúc thư mục res trong Android
Trước tiên mình có một hình ảnh của thư mục res và các thư mục con của nó khi tạo project mặc định (Empty Activity) sử dụng Android Studio.
Chúng ta thấy rằng có các thư mục như drawable, layout, mipmap, values. Ngoài ra các bạn có thể tạo ra các thư mục như anim, raw. Và dươi đây mình sẽ giải thích ý nghĩa của từng thư mục trên
Tên thư mục | Ý nghĩa |
drawable | Là thư mục chứa drawable (hình ảnh, shape, vector) của ứng dụng. Chi tiết hơn về Drawable mình đã viết một bài khá là chi tết về nó ở bài viết Drawable trong Android. |
layout | Thư mục này chứa định nghĩa layout của ứng dụng. Ví dụ như layout của activity, fragment… |
mipmap | Thư mục chứa icon của ứng dụng. |
raw | Thư mục chứa các file raw |
anim | Thư mục chứa các animation được định nghĩa bằng xml. |
values | Thư mục này chứa các file xml định nghĩa các loại resoure khác nhau:
+ attrs.xml: Định nghĩa các thuộc tính sử dụng cho việc tạo view mới. (CustomView). + color.xml: Định nghĩa màu sắc được sử dụng trong ứng dụng. + dimen.xml: Định nghĩa các giá trị về kích thước, font chữ và thường có đơn vị là dp, dip, sp, px. + strings.xml: Chứa các chuỗi được sử dụng trong app. Ví dụ như tên app, tên button, text của TextView… + styles.xml: Định nghĩa Theme, Style cho view. Ngoài ra các bạn có thể tạo ra file xml khác như: + bool.xml: Chứa các giá trị boolean. + integers.xml: Chứa các giá trị số. + arrays.xml: Chứa các mảng. |
Trên đây là ý nghĩa của nhưng thư mục nằm trong thư mục res (resource) của project trong Android. Để sử dụng chúng trong xml của như Java như thế nào. Các bạn cùng xem phần dưới đây.
Sử dụng Resource trong Android
Trong Xml
Hầu hết trong file layout chúng ta đều có thể truy cập resource thông qua
1 |
@resource_type/value |
resource_type thường có các giá trị sau:
- drawable
- color
- dimen
- string
- style
- bool
- integer
và value có các giá trị:
- id của file (thường sử dụng trong trường hợp drawable).
- Trường name định nghĩa trong file đó. Thường được dùng trong các file nằm trong thư mục values.
Ví dụ tôi có các file như sau:
File dimens.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <!--define value diment for tvContent--> <dimen name="text_view_content_width">40dp</dimen> <dimen name="text_view_content_height">40dp</dimen> <dimen name="text_view_content_margin_left">3dp</dimen> <dimen name="text_view_content_margin_right">3dp</dimen> <dimen name="text_view_content_margin_top">8dp</dimen> <dimen name="text_view_content_margin_bottom">8dp</dimen> <dimen name="text_view_content_padding_left">10dp</dimen> <dimen name="text_view_content_padding_right">10dp</dimen> <dimen name="text_view_content_padding_top">6dp</dimen> <dimen name="text_view_content_padding_bottom">6dp</dimen> <!--define text size for tvContent--> <dimen name="text_content_text_size">16sp</dimen> </resources> |
File strings.xml
1 2 3 4 5 |
<resources> <string name="app_name">DemoResourceInAndroid</string> <string name="text_content">Eitguide.com</string> </resources> |
File colors.xml
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="red">#ff0000</color> </resources> |
File styles.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="TextViewContentStyle"> <item name="android:textStyle">bold</item> </style> </resources> |
Và dưới đây là các sử dụng resoure định nghĩa ở trên.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<TextView style="@style/TextViewContentStyle" android:layout_width="@dimen/text_view_content_width" android:layout_height="@dimen/text_view_content_height" android:layout_marginBottom="@dimen/text_view_content_margin_bottom" android:layout_marginLeft="@dimen/text_view_content_margin_left" android:layout_marginRight="@dimen/text_view_content_margin_right" android:layout_marginTop="@dimen/text_view_content_margin_top" android:paddingBottom="@dimen/text_view_content_padding_bottom" android:paddingLeft="@dimen/text_view_content_padding_left" android:paddingRight="@dimen/text_view_content_padding_right" android:paddingTop="@dimen/text_view_content_padding_top" android:text="@string/text_content" android:textColor="@color/red" android:textSize="@dimen/text_content_text_size" /> |
Ngoài ra chúng ta có thể tạo thêm các resource khác như file trong thư mục values của project như bools.xml (chứa các giá trị logic), integers.xml (chứa các giá trị số nguyên), string_arrays.xml (chứa mảng kiểu string).
Ví dụ
File bools.xml
1 2 3 4 5 |
<?xml version="1.0" encoding="utf-8"?> <resources> <bool name="Checked">true</bool> <bool name="NotChecked">false</bool> </resources> |
Sử dụng cho những View nào có thuộc tính chấp nhận là true hay false. Ở đây mình sử dụng CheckBox
1 2 3 4 |
<CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="@bool/Checked"/> |
File integers.xml
1 2 3 4 5 |
<?xml version="1.0" encoding="utf-8"?> <resources> <integer name="minimum">0</integer> <integer name="maximum">100</integer> </resources> |
Và được sử dụng cho những thuộc tính của view chấp nhận giá trị kiểu int. Ở đây mình sử dụng SeekBar.
1 2 3 4 5 |
<SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:max="@integer/maximum" android:progress="@integer/minimum" /> |
Sử dụng lại resource để định nghĩa resource.
Chúng ta hoàn toàn có thể sử dụng lại những resource đã định nghĩa trước để định nghĩa lại resource mới.
Ví dụ
1 2 3 4 5 6 7 8 9 |
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <dimen name="circle_bound_width">20dp</dimen> <dimen name="circile_bound_height">@dimen/circle_bound_width</dimen> </resources> |
Một ví dụ khác nữa tôi định nghĩa color và sử dụng color này để định nghĩa style
File colors.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="white">#ffffff</color> <color name="red">#ff0000</color> <color name="green">#00ff00</color> </resources> |
File styles.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="TextViewStyle"> <item name="android:textColor">@color/red</item> <item name="android:background">@color/green</item> </style> </resources> |
Trong Java
Trong Java để truy cập vào resource trong java chúng ta phải thông qua R.resource_type.value. Với resource_type và value đã giải thích ở phần trên. Và sử dụng phương thức
1 |
getResources().getResourcesType(R.resource_type.value); |
Với getResourcesType là các phương thức:
- getColor
- getDrawable
- getInteger
- getBoolean
- getDimension
- getString
- …
Các bạn có thể gõ “getResources().” để Android Studio gợi ý cho chúng ta các phương thức get các loại resource.
Ví dụ tôi get một số resource đã sử dụng trong bài viết:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//get color resource int color = getResources().getColor(R.color.red); //get integer resource int max = getResources().getInteger(R.integer.max); //get boolean resource boolean checked = getResources().getBoolean(R.bool.Checked); //get dimen resource int margin = (int)getResources().getDimension(R.dimen.activity_horizontal_margin); //get drawable resource Drawable drawable = getResources().getDrawable(R.drawable.icon_what_app); //get String resource String str = getResources().getString(R.string.app_name); |
Kết luận
Trên đây tôi đã giới thiệu chi tết về resources trong Android. Mong rằng qua bài viết này các bạn có thể vận dụng những kiến thức mà tôi truyền đạt vào công việc của các bạn. Nếu có bất cứ thắc mắc nào có thể để lại bình luận ở phía dưới bài viết hoặc qua fanpage Eitguide Android để được giải đáp.