Android Study Notes 15: Use Android Application Resources

Zero, learning goals

  1. Will use string resources in layouts or programs
  2. Will use array resources in layouts or programs
  3. Will use color resources in layouts or programs
  4. Will use image resources in layouts or programs
  5. Will use Android application audio resources in the program

1. Resource description

Insert picture description here

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 mipmapdirectory
  • Image resources are generally placed in the drawabledirectory

5. Audio resources

  • Audio resources included in the application are generally placed in a self-built rawdirectory
  • 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

Insert picture description here

  • 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

  1. Linear layout
  2. Button
  3. String resource
  4. Array resource
  5. Color resource
  6. Audio resources (play)

(3) Implementation steps

1. Create an Android application [UseAppResources]

Insert picture description here
Insert picture description here

2. Copy the picture material to the drawable directory

Insert picture description here

  • 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

Insert picture description here

  • 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

Insert picture description here

<?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

Insert picture description here

(1) String resources

Insert picture description here

  • 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

Insert picture description here

  • 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

Insert picture description here

  • 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.
    Insert picture description here
    ● 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

Insert picture description here

  • Declare variable
    Insert picture description here
  • Get control instance by resource identifier
    Insert picture description here
  • Access string resource button click event processing method
    Insert picture description here
  • Access to string array resource button click event processing method
    Insert picture description here
  • Change background color button click event processing method
    Insert picture description here
  • Change the background image button click event processing method
    Insert picture description here
  • Handling method of play music button click event
    Insert picture description here

8. Start the application and check the effect

Insert picture description here

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.
    Insert picture description here
  • 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.pngresource, android.R.drawable.btn_check_offyou can access it by using it in the layout resource file .
    Insert picture description here

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.

Guess you like

Origin blog.csdn.net/howard2005/article/details/109153807