首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

Android Development for Beginner

2024-12-17 来源:花图问答

Lesson 3A Object Oriented Programming

Content

  • 1.Resources and Java Code
  • 2.Resources Overview
  • 3.Resource Types
  • 4.Providing Resources
  • 5.Accessing Resources
  • 6.From XML to Java
  • 7.Reading and Writing Logs

1. Resources and Java Code

Resources live under the res directory of your app. Res is short for resources. On the other hand, java files live in the java folder. And we have MainActivity.java located inside this folder.
An Android app is mainly made of resources and java code. The java code consists of files like MainActivity.java. Altogether it handles what should happen when the app is running like responding to button clicks or other events. Then you have resources that live in the res directory of your app. This includes images, text strings, colors, dimensions, like for width and height, xml files like for menus and layouts, as well as raw media files like for audio or video.
One of the major advantages to separating out the java code and the resources is that we can provide alternative resources to make the app look better on certain devices. So if we want a different layout on larger screen devices, we can provide that by just swapping it out.Or, if we want to translate these strings into another language, then it makes it easier to swap out the strings in just the centralized location instead of having it scattered all over the code files.

Resources and Java Code

You should always externalize resources such as images and strings from your application code, so that you can maintain them independently. Externalizing your resources also allows you to provide alternative resources that support specific device configurations such as different languages or screen sizes, which becomes increasingly important as more Android-powered devices become available with different configurations. In order to provide compatibility with different configurations, you must organize resources in your project's res/ directory, using various sub-directories that group resources by type and configuration.

Each of the documents in this section describe the usage, format and syntax for a certain type of application resource that you can provide in your resources directory ( res/ ).
Here's a brief summary of each resource type:


  • Define pre-determined animations. Tween animations are saved in res/anim/ and accessed from the R.anim class.
    Frame animations are saved in res/drawable/ and accessed from the R.drawable class.

  • Define a color resources that changes based on the View state.Saved in res/color/ and accessed from the R.color class.

  • Define various graphics with bitmaps or XML.
    Saved in res/drawable/ and accessed from the R.drawable class.

  • Define the layout for your application UI.
    Saved in res/layout/ and accessed from the R.layout class.

  • Define the contents of your application menus.
    Saved in res/menu/ and accessed from the R.menu class.

  • Define strings, string arrays, and plurals (and include string formatting and styling).
    Saved in res/values/ and accessed from the R.string, R.array , and R.plurals classes.

  • Define the look and format for UI elements.
    Saved in res/values/ and accessed from the R.style class.

  • Define values such as booleans, integers, dimensions, colors, and other arrays.
    Saved in res/values/ but each accessed from unique R sub-classes (such as R.bool , R.integer, R.dimen, etc.).

This part shows you how to group your resources in your Android project and provide alternative resources for specific device configurations.

Quickview

  • Different types of resources belong in different subdirectories of res/
  • Alternative resources provide configuration-specific resource files
  • Always include default resources so your app does not depend on specific device configurations

Access Resource

XML&JAVA Creat Object Creat Object

  • The Log class
    is a logging class that you can utilize in your code to print out messages to the LogCat. Common logging methods include:
    • [v(String, String)](, java.lang.String)) (verbose)
    • [d(String, String)](, java.lang.String)) (debug)
    • [i(String, String)](, java.lang.String)) (information)
    • [w(String, String)](, java.lang.String)) (warning)
    • [e(String, String)](, java.lang.String)) (error)

For example:
Log.i("MyActivity", "MyClass.getView() — get item number " + position);
The LogCat will then output something like:
I/MyActivity( 1557): MyClass.getView() — get item number 1

  • Filtering Log Output
    Every Android log message has a tag and a priority associated with it.
    • The tag of a log message is a short string indicating the system component from which the message originates (for example, "View" for the view system).
    • The priority is one of the following character values, ordered from lowest to highest priority:
    • V — Verbose (lowest priority)
    • D— Debug
    • I— Info
    • W— Warning
    • E — Error
    • F— Fatal
    • S — Silent (highest priority, on which nothing is ever printed)

You can obtain a list of tags used in the system, together with priorities, by running LogCat and observing the first two columns of each message, given as <priority>/<tag>.
Here's an example of logcat output that shows that the message relates to priority level "I" and tag "ActivityManager":
I/ActivityManager( 585): Starting activity: Intent {action=android.intent.action...}

Sample Code :

<LinearLayout 
    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/menu_item_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mango sorbet"
        android:textAppearance="?android:textAppearanceMedium" />

    <TextView
        android:id="@+id/menu_item_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Blueberry pie"
        android:textAppearance="?android:textAppearanceMedium"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/menu_item_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Chocolate lava cake"
        android:textAppearance="?android:textAppearanceMedium"
        android:textSize="18sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:onClick="printToLogs"
        android:text="Print menu to logs" />
</LinearLayout>
package com.example.android.menu;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void printToLogs(View view) {
        // Find first menu item TextView and print the text to the logs
        TextView textViewItem1 = (TextView) findViewById(R.id.menu_item_1);
        String menuItem1 = textViewItem1.getText().toString();
        Log.v("MainActivity", menuItem1);

        // Find second menu item TextView and print the text to the logs
        TextView textViewItem2 = (TextView) findViewById(R.id.menu_item_2);
        String menuItem2 = textViewItem2.getText().toString();
        Log.v("MainActivity", menuItem2);

        // Find third menu item TextView and print the text to the logs
        TextView textViewItem3 = (TextView) findViewById(R.id.menu_item_3);
        String menuItem3 = textViewItem3.getText().toString();
        Log.v("MainActivity", menuItem3);
    }
}

Output:


Output
显示全文