Article Directory
- Zero, learning goals
- 1. Resource description
- 2. Case demonstration-using Android application resources
-
- (1) Operation effect
- (2) Involving knowledge points
- (3) Implementation steps
-
- 1. Create an Android application [UseAppResources]
- 2. Copy the picture material to the drawable directory
- 3. Copy the audio resources to the self-built raw directory
- 4. The main layout resource file activity_main.xml
- 5. String resource file strings.xml
- 6. Color resource file colors.xml
- 7, the main interface class MainActivity
- 8. Start the application and check the effect
- 9. Optimized code description
- 3. Access Android platform resources
- Four, homework
Zero, learning goals
- Will use string resources in layouts or programs
- Will use array resources in layouts or programs
- Will use color resources in layouts or programs
- Will use image resources in layouts or programs
- Will use Android application audio resources in the program
1. Resource description
1. String resources
- In the string resource file
strings.xml
, use<string>
elements to define string resources
2. Array resources
- In the string resource file
strings.xml
, the use of<array>
,<integer-array>
or<string-array>
the elements to define an array of resources
3. Color resources
- In the color resource file
colors.xml
, use<color>
elements to define color resources
4. Picture resources
- Icon resources are generally placed in the
mipmap
directory - Image resources are generally placed in the
drawable
directory
5. Audio resources
- Audio resources included in the application are generally placed in a self-built
raw
directory - Application external audio resources are generally stored on an external memory card
- Android applications can access audio on the Internet (via
URI
)
2. Case demonstration-using Android application resources
-
Task: Use Android application resources, including strings, arrays, colors, pictures, and audio.
-
Regarding the style resource styles, it is a bit of CSS in web design. It is an interface beautifier. Of course it is also very useful. We will talk about it later.
(1) Operation effect
- Click the [Play Music] button, you will hear the music sound, but now we do not control the music playback, such as pause, stop and other functions we will learn how to achieve.
(2) Involving knowledge points
- Linear layout
- Button
- String resource
- Array resource
- Color resource
- Audio resources (play)
(3) Implementation steps
1. Create an Android application [UseAppResources]
2. Copy the picture material to the drawable directory
- Access the image resource bg1.jpg in the layout resource file:
android:background="@drawable/bg1"
- Access the image resource bg1.jpg in Java code:
Drawable bg1 = getResources().getDrawable(R.drawable.bg1);
3. Copy the audio resources to the self-built raw directory
- Access audio files in Java code:
MediaPlayer mp = MediaPlayer.create(this, R.raw.alice);
- Android applications can access the audio in the project, can also access the audio on the memory card, and even access the audio resources on the network. At present, we only talk about the first situation.
4. The main layout resource file activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btnAccessString"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doAccessString"
android:text="@string/access_string"
android:textColor="#0000ff"
android:textSize="20sp" />
<Button
android:id="@+id/btnAccessStringArray"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doAccessStringArray"
android:text="@string/access_string_array"
android:textColor="#0000ff"
android:textSize="20sp" />
<Button
android:id="@+id/btnChangeBackColor"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doChangeBackColor"
android:text="@string/change_backcolor"
android:textColor="#0000ff"
android:textSize="20sp" />
<Button
android:id="@+id/btnChangeBackground"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doChangeBackground"
android:text="@string/change_background"
android:textColor="#0000ff"
android:textSize="20sp" />
<Button
android:id="@+id/btnPlayMusic"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doPlayMusic"
android:text="@string/play_music"
android:textColor="#0000ff"
android:textSize="20sp" />
</LinearLayout>
5. String resource file strings.xml
(1) String resources
- Access the college variable in the layout resource file:
android:text="@string/college"
- Access the college variable in the Java source code:
String strCollege = getResources().getString(R.string.college);
(2) Array resources
- Access the majors variable in the layout resource file:
android:entries="@array/majors"
- Access the majors variable in Java code:
String[] strMajors = getResources().getStringArray(R.array.majors);
6. Color resource file colors.xml
- A lot of colors are used in an application. If you use "#XXXXXX" to set the colors, it is not intuitive and not easy to use in the program. For this reason, we should create various colors required by the program in the color resource file , Can achieve the purpose as the name suggests.
● Access color variables in layout resource files:android:background="@color/red"
● Access color variables in Java code:int color = getResources().getColor(R.color.red)
7, the main interface class MainActivity
- Declare variable
- Get control instance by resource identifier
- Access string resource button click event processing method
- Access to string array resource button click event processing method
- Change background color button click event processing method
- Change the background image button click event processing method
- Handling method of play music button click event
8. Start the application and check the effect
9. Optimized code description
- Initialize the color array, not in the event handling method, but in the onCreate method
- Initialize the image array, not in the event handling method, but in the onCreate method
3. Access Android platform resources
- Android includes a large number of standard resources, such as pictures, styles, themes, and layouts.
- To access these resources, you need to add the android package name before the resource reference. For example, to access the
res/drawale-mdpi/btn_check_off.png
resource,android.R.drawable.btn_check_off
you can access it by using it in the layout resource file .
Four, homework
Task: Make a personal photo album
- Make a simple personal photo album, provide [previous] and [next] buttons to switch pictures, with beautiful background music.
- When switching to the first picture, if you click [Previous] again, then switch to the last picture;
- When switching to the last picture, if you click [Next] again, then switch to the first picture.
- A text description is required under each picture.