1.8 Project-related analysis (various files, resource access

Table of contents

 

1.8 Project-related analysis (various files, resource access)

Classification Android basic introductory tutorial

Introduction to this section:

1. Engineering project structure analysis:

1.res resource folder introduction:

2. How to use these resources

2. Get an in-depth understanding of the three documents:

MainActivity.java:

Layout file: activity_main.xml:

AndroidManifest.xml configuration file:

Summary of this section:


 

1.8 Project-related analysis (various files, resource access

Introduction to this section:

I talked about a lot of things that seem to have nothing to do with our Android development, right? Of course, it just seems like it now. You will know it later when you look back! Well, in this section we will use the Hello World project created earlier as an entry point to understand the project structure and the two ways of resource access in Android! The IDE used in subsequent tutorials is Android Studio, because Google officially announced a few days ago that it will terminate support for other IDE development environments before the end of the year!

20230724024159.png?origin_url=%2F%2Fwww.runoob.com%2Fwp-content%2Fuploads%2F2015%2F06%2F85916312.jpg&pos_id=HPBxVi5Z


1. Engineering project structure analysis:

Most of our development time is spent on the following part:

20230724024159.png?origin_url=%2F%2Fwww.runoob.com%2Fwp-content%2Fuploads%2F2015%2F06%2F31508278.jpg&pos_id=QtuF57dH

Next we will explain the key parts:

  • java: This is where we write Java code and business functions are implemented here.
  • res: where our various resource files are stored, including pictures, character strings, animations, audio, etc., as well as various forms of XML files

1.res resource folder introduction:

PS: Speaking of the res directory , there is also the assets directory . Although it does not exist here, we can create it ourselves. The difference between the two is whether all resource files under the former will generate corresponding resources under the R.java file. id, but the latter does not; in the former, we can directly access the corresponding resource through the resource id; while in the latter, we need to read it in the form of a binary stream through AssetManager! By the way, this R file can be understood as a dictionary, and each resource under res will generate a unique ID here!

Next, let’s talk about the relevant directories under the res resource directory:

PS: The following mipmap directories do not exist in Eclipse. In Eclipse, they all start with drawable. In fact, there is not much difference. It is just that using mipmap will provide certain performance optimization during image scaling. The system will distinguish according to the screen resolution. Rate to select the corresponding pictures under hdpi, mdpi, xmdpi, xxhdpi, so when you unzip other people's apk, you can see the pictures with the same name in the above directory, which are available in the four folders, but the size and pixels are different! Of course, this is not absolute. For example, if we put all the pictures under drawable-hdpi, even if the mobile phone should load the picture resources under the ldpi folder, but there is no under ldpi, then the ones loaded will still be under hdpi. pictures! In addition, there is another situation: for example, if it is hdpi, it is in the mdpi directory, but not in ldpi, then the resources in mdpi will be loaded! The principle is to use the closest density level! In addition, if you want to prohibit Android from not following the screen density To load resources from different folders, just add the android:anyDensity="false" field to the AndroidManifest.xml file!

1. Let’s talk about picture resources first :

  • drawable: stores various bitmap files, (.png, .jpg, .9png, .gif, etc.) In addition, it may be some other drawable type XML files
  • mipmap-hdpi: high resolution, usually we throw pictures here
  • mipmap-mdpi: medium resolution, rarely unless the compatible phone is very old
  • mipmap-xhdpi: Ultra-high resolution, mobile phone screen materials are getting better and better, and it is estimated that it will gradually transition to this in the future
  • mipmap-xxhdpi: Ultra-high resolution, this is reflected in high-end machines

2. Let’s talk about layout resources:

  • layout: What is stored in this directory is our layout file. In addition, on some specific models, we do screen adaptation, such as 480*320 mobile phones, we will create another set of layouts, just like this: layout-480x320 folder!

3. Next let’s talk about menu resources:

  • Menu: In the past, mobile phones with physical menu buttons, that is, the menu key, were used more often, but now they are not used much. The resource xml related to the menu items can be written here. I wonder if Google will come up with something new. Replacement menu~

4. Next, let’s talk about the values ​​directory:

  • demens.xml: define size resources
  • string.xml: Define string resources
  • styles.xml: Define style resources
  • colors.xml: Define color resources
  • arrays.xml: Define array resources
  • attrs.xml: Used more when customizing controls, customizing the properties of controls!
  • theme theme file is very similar to styles, but it will affect the Actvitiy or specified Activity in the entire application, generally changing the appearance of the window! It can be used in Java code through setTheme, or add theme attributes to <application...> in Androidmanifest.xml! PS: You may have seen such values ​​directories: values-w820dp, values-v11, etc. In the former, w represents the tablet device, and 820dp represents the screen width; while v11 means that it will be used after API (11), that is, Android 3.0. of!

5. Next, let’s talk about this raw directory: used to store various native resources (audio, video, some XML files, etc.), we can obtain the binary stream of resources through openRawResource (int id)! In fact, it is similar to Assets, but the resources here will generate a resource ID in the R file.

6. Finally, there is animation. There are two types of animation: attribute animation and tweening animation:

  • animator: XML file that stores attribute animations
  • anim: XML file that stores tween animations

2. How to use these resources

Well, now that we know what resources there are, let’s learn how to use them: As mentioned before, all our resource files will generate a resource ID under the R.java file. We can use this resource ID to complete resource access. , there are two usage situations: used in Java code and used in XML code.

Used in Java code:

Java characters:

txtName.setText(getResources().getText(R.string.name));

picture:

imgIcon.setBackgroundDrawableResource(R.drawable.icon);

color:

txtName.setTextColor(getResouces().getColor(R.color.red));

layout:

setContentView(R.layout.main);

Controls:

txtName = (TextView)findViewById(R.id.txt_name);

Used in XML code:

You can get it through @xxx, for example, get text and pictures here:

<TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background = "@drawable/img_back"/>

2. Get an in-depth understanding of the three documents:

Okay, next we will analyze the three more important files in the project: MainActivity.java, layout file: activity_main and Android configuration file: AndroidManifest.xml PS: The content of the pictures may be a little different, and I don’t have time to make pictures. I hope you can understand~

20230724024159.png?origin_url=%2F%2Fwww.runoob.com%2Fwp-content%2Fuploads%2F2015%2F06%2F53105279.jpg&pos_id=luCxpBM8

MainActivity.java:

code show as below

package jay.com.example.firstapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Code analysis:

20230724024159.png?origin_url=%2F%2Fwww.runoob.com%2Fwp-content%2Fuploads%2F2015%2F06%2Factivity_main.jpg&pos_id=wCfaubjF

Layout file: activity_main.xml:

code show as below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>
 

Code analysis:

 

We define a LinearLayout linear layout and define the architecture we need to use in the xml namespace, from ①

AndroidManifest.xml configuration file:

code show as below:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jay.com.example.firstapp" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Code analysis:

In addition to the above:

① If the app contains other components, they must be declared in the file using type description syntax Server: element BroadcastReceiver element ContentProvider element IntentFilter <intent-filter> element

②Declaration of permissions: Explicitly declare the permissions required by the program in this file to prevent the app from incorrectly using services and improperly accessing resources, ultimately improving the robustness of the android app. android.permission.SEND_SMS This sentence indicates that the app needs Using the permission to send information, the user will be prompted during installation. The relevant permissions can be found in the sdk reference manual!


Summary of this section:

In this section we have a detailed understanding of our Hello World project, what the relevant directories do, what are the resource files under res, what are their functions, and how to use these resources! At the same time, the three most important files in the project are explained in detail by drawing pictures! At this point, I believe you have a general understanding of the Android project! Thank you~

 

Guess you like

Origin blog.csdn.net/2301_78835635/article/details/132647054