[Android Series-] 2. Android project directory structure and user interface creation

Source: Internet
Author: User

Preface

In [Android Series-] 1. Android development Environment Building and Hello World This article describes how to quickly build an Android development environment and successfully build an Android application without any code changes.

Next, you have to see

1. What directories and files are available for apps created with Eclipse

2. How to create a user interface that is more advanced than Hello World



appcompat_v7

When you create an Android project for Myfirstapp, you find that there is an extra APPCOMPAT_V7 project under the project path.

And this project can not be deleted, after deletion, the new project will also error.

APPCOMPAT_V7 is a Compatibility Pack for Google, a support library that allows more than 2.1 of the 4.0 version of the interface to be used.


How to remove?

This support library is not required if the new project is to select the minimum SDK above Andorid4.0.


So, for the time being, leave it there, and let it be there.


directory Structure



1. src

Store the developed Java source code files.

By default, an Activity class is included, and the app runs into the class.

2. Gen

Some Java code generated automatically by the compiler.

The most critical file in this directory is R.java, which is a read-only mode and cannot be changed.

The R class contains many static classes, and the name of the static class corresponds to a name in Res, which is like a resource dictionary encyclopedia. It contains the user interface, images, strings and other resources corresponding to the identifier, R class defines the index of all resources of the project. For example, there is a text control in the interface, and the control is android:id= "@+id/textview" in the layout file, so the control can be found by R.id.textivew. Id.id.

With R.java you can quickly find the resources you need, and the compiler will check if the resources in the R.java list are being used, and the unused resources will not be compiled into the software, which will reduce the space occupied by the phone.

The R.java default has attr,drawable,layout,string 4 static inner classes (Android 4.2 has 8 static classes, more id,menu,style,dimen), and each class corresponds to a resource. For example, if we add a picture to the project, the project will add a piece of data to the drawable inner class of this class, and the project will automatically delete the data if the image is deleted. This shows that the R.java is similar to the computer's registration form.


3. Android 4.4.2,

Android Private Libraries

Android Dependencies

These three are libraries.

What you need to specifically note is the Android Dependencies

This directory appears in the ADT16 version, is the ADT third-party library new reference method, when we need to reference a third-party library, only need to copy the library to the Libs folder, ADT will automatically complete a reference to the library (as in this example, Android-support-v4.jar)


4. assests
In addition to providing a res directory to store resource files, Android can also store resource files in the assets directory. The resource files in the assets directory are no longer r.java automatically generated IDs, so the files under the Asset directory must specify the path to the file, which can be accessed through the Assetmanager class


5. Bin

This directory is the compiled file and some intermediate files in the directory, ADT first compiled the project into the Android Java Virtual Machine (Dalvik) file Classes.dex. Finally, the classes.dex is encapsulated into an APK package. (APK is the installation package that the Android platform is born on).


6. Libs

This directory is used to store third-party libraries, which are generated by default when you create a new project, and are created manually if you do not.


7. Res

The resource files in the project are stored and R.java are automatically recorded when resources are added to the directory. The following subdirectories are commonly found in the Res directory

DRAWABLE-HDPI, drawable-mdpi, drawable-xhdpi: Storing picture files (PNG, JPG), three subdirectories save High, medium, and low quality pictures respectively

Layout: Screen layout directory, layout directory, the default layouts file is Activity_main.xml, you can put different layout structures and controls within the file to meet the needs of the project interface, you can also create a new layout file.

Menu: Holds the XML file that defines the Application menu resource.

Values, Values-v11, VALUES-V14: Stores XML files that define multiple types of resources.


8. Androidmanifest.xml

A manifest file that describes the basic characteristics and composition of the application.

The most important element is <USES-SDK>, which defines the compatibility settings of the application for different versions of Android android:minSdkVersion android:targetSdkVersion .

Similar:

<manifest xmlns:android= "Http://schemas.android.com/apk/res/android" ... >    <uses-sdk android: minsdkversion= "8" android:targetsdkversion= "/>"    ...</manifest>
Actual development: Android:targetsdkversion This value may often be set to the current version.


user Interface

The user interface of the Android app is built on top of the View and ViewGroup level objects.

The View object is usually a UI component like buttons or text fields;

The ViewGroup object is an invisible view container that defines the layout of the child's views, such as a grid or a vertical list.

You can define the hierarchical structure of the UI in XML:


Create a user interface

Open Res/layout/fragment_main.xml.

The contents of this file are:

<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 "    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=" Com.oscar999.myfirstapp.mainactivity$placeholderfragment ">    <textview        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content        " android:text= "@string/hello_world"/></relativelayout>

The show Hello world should be the place to be configured.

Next, we'll show you how to create a new user interface.

1. Create a linear layout

Res/layout/fragment_main.xml


Delete the TextView element;

Change the relativelyaout to LinearLayout and add the Android:orientation property with the value "horizontal"

The modified content is:

<linearlayout 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 "    android:orientation= "Horizontal" ></LinearLayout>

LinearLayout is a view group (a subclass of ViewGroup) that is configured in a horizontal or vertical direction by the Android:orientation property configuration.

Both the Android:layout_width and Android:layout_height properties are required to specify the size. Because Linerlayout is the layout root view, it needs to populate this screen area, so it's set to "Match_parent", which allows the view to automatically stretch to fit the size of the parent view.


2. Add a TextBox (text Field)

Use the <EidtText> element to add a text box that you can enter in Lienarlayout.

The contents are as follows:

   <edittext android:id= "@+id/edit_message"        android:layout_width= "wrap_content"        android:layout_height= " Wrap_content "        android:hint=" @string/edit_message "/>

Android:id:

Defines a unique identity that can be used by this ID in the app code.

@--This is required, followed by the ID, and then the resource name (edit_message)

+--The plus sign is only required when the resource ID is first defined.


Android:hint

The default value for the input box.

@string/edit_message This is a reference to a string resource that has already been defined.


3. Adding a string resource

The above is useful to string resources (Edit_message), where is this defined?

Open: Res/values/strings.xml, you will find that Hello world is defined in this place.

Add two lines at the end



4. Add a button

Like the text Field above, add a button,

    <button        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:text= " @string/button_send "/>
Save the above modified XML file and rerun the app


Make the input box fill the entire screen width
From the above display effect can be seen, the width of the button is OK, the width of the input box is slightly smaller, if you can expand to the entire screen is good.

The configuration for modifying EditText is as follows:

Layout_weight is configured with a weight value to configure the proportion of the remaining space that each view occupies.

If there are two view, one value is 1, one value is 2, then one occupies 3/1 and the other occupies 2/3. The default value is 0,

Android:layout_width= "0DP", configured to 0, is to improve the performance of the layout, because the "wrap_content" configuration, the system will calculate the corresponding size.

After the completion of the effect:




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.