Android APP简述

由于本人并非专业Android APP专业人士,本文所记录的APP的结构信息仅仅作为本人笔记使用。

一 Android APP 概览:

Android 的app 存放于 packages/apps/下 ,例如,创建APP文件 test。test文件夹中的目录结构,文件分布如下:

在这里插入图片描述

1 res目录

res文件夹:存放了android应用所用的全部资源,包括图片资源、字符串资源、颜色资源、尺寸资源等。

1.以drawable开头的目录:用来存放图片 
2.mipmap开头的目录:用于存放应用图标 
3.values开头的文件夹用来存放字符串,样式,颜色等配置 
4.layout用于存放布局文件
2 src目录

src文件夹:存放app源码,监听app上面的按键,发送广播 等等

3 Android.mk

Android.mk是Android提供的一种makefile文件,用来指定诸如编译生成so库名、引用的头文件目录、需要编译的.c/.cpp文件和.a静态库文件

4 AndroidManifest.xml

AndroidManifest.xml 是每个android程序中必须的文件。它位于整个项目的根目录,描述了package中暴露的组件(activities, services, 等等),他们各自的实现类,各种能被处理的数据和启动位置。 除了能声明程序中的Activities, ContentProviders, Services, 和Intent Receivers,还能指定permissions和instrumentation(安全控制和测试)

Android.mk

Android.mk是Android提供的一种makefile文件,用来指定诸如编译生成so库名、引用的头文件目录、需要编译的.c/.cpp文件和.a静态库文件,如下:

LOCAL_PATH:= $(call my-dir)  //每个Android.mk文件必须以定义LOCAL_PATH为开始。它用于在开发tree中查找源文件

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional  //LOCAL_MODULE_TAGS :=optional 的模块会被编译,但不会安装到image里面

LOCAL_SRC_FILES := $(call all-java-files-under, src) //如果要包含的是java源码的话,可以调用all-java-files-under得到。(这种形式来包含local_path目录下的所有java文件);

LOCAL_PACKAGE_NAME := testappmhr //package的名字,这个名字在脚本中将标识这个app或package。

LOCAL_CERTIFICATE := platform

LOCAL_DEX_PREOPT := false //这个变量设置为false可以使整个系统使用提前优化的时候,某个app不使用提前优化。

include $(BUILD_PACKAGE) //#编译生成apk

# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))

三 AndroidManifest.xml

AndroidManifest.xml 是每个android程序中必须的文件。它位于整个项目的根目录,描述了package中暴露的组件(activities, services, 等等),他们各自的实现类,各种能被处理的数据和启动位置。 除了能声明程序中的Activities, ContentProviders, Services, 和Intent Receivers,还能指定permissions和instrumentation(安全控制和测试)
如下例子:

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

		  
	<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATIONS"/>
		  
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
		android:exported="true"
        android:supportsRtl="true"
		android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

二 res目录

res目录结构如下:
在这里插入图片描述
res目录我们主要关注 layout 目录和value目录。

1 layout目录

该目录下的 xxxx.xml 文件的作用就是把APP UI界面中的控件按照某种规律摆放到指定的位置。就是负责APP控件(按键等控件)的摆放。如下:
在这里插入图片描述

如下为 本APP控件摆放位置

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="mhr.video.com.alarmshutdown.AlarmActivity">


   <TextView
        android:text="@string/starttime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView_time_on"
		android:layout_marginStart="170dp"
        android:layout_alignParentTop="true"/>
		
	<TextView
        android:text="@string/shutdowntime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView_time_off"
		android:layout_marginStart="1000dp"
		android:layout_alignParentTop="true"/>	
	
	<DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
		android:calendarViewShown="false"
        android:id="@+id/datePicker_on"
		android:layout_below="@+id/textView_time_on"
		android:layout_marginStart = "5dp"/>
		
	<DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
		android:calendarViewShown="false"
        android:id="@+id/datePicker_off"
		android:layout_below="@+id/textView_time_off"
		android:layout_marginStart="800dp" />

	<TimePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/timePicker_on"
        android:layout_below="@+id/textView_time_on"
		android:layout_toRightOf = "@+id/datePicker_on" />
		
	<TimePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/timePicker_off"
        android:layout_below="@+id/textView_time_off"
		android:layout_toRightOf = "@+id/datePicker_off" />
		
	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_cancel_timeon"
        android:text="@string/canle"
        android:layout_below="@+id/timePicker_on"
		android:layout_alignRight = "@+id/timePicker_on"/>
		
	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_set_timeon"
        android:text="@string/set"
        android:layout_below="@+id/datePicker_on"
		android:layout_alignLeft="@+id/datePicker_on"/>
		
	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_cancel_timeoff"
        android:text="@string/canle"
        android:layout_below="@+id/timePicker_off"
		android:layout_alignRight="@+id/timePicker_off"/>
	
	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_set_timeoff"
        android:text="@string/set"
        android:layout_below="@+id/datePicker_off"
		android:layout_alignLeft="@+id/datePicker_off"/>
		
	<TextView
        android:text="@string/Remote_OnOff_LCD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Remote_OnOff_LCD"
		android:layout_marginStart="170dp"
		android:layout_marginTop="50dp" 
        android:layout_below="@+id/button_set_timeon"/>
		
	<Button
        android:text="@string/enable_backlight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
		android:id="@+id/button_open_lcd"
        android:layout_below="@+id/Remote_OnOff_LCD"
		android:layout_marginTop="30dp" 
        android:layout_marginStart = "5dp"/>
        
	<Button
        android:text="@string/disable_backlight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
		android:id="@+id/button_close_lcd"
        android:layout_below="@+id/Remote_OnOff_LCD"
		android:layout_marginTop="30dp" 
		android:layout_alignRight = "@+id/button_cancel_timeon"/>
		
	<TextView
        android:text="@string/control_wtd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/control_wtd"
		android:layout_marginTop="50dp"
		android:layout_below="@+id/button_set_timeoff"
		android:layout_marginStart="1000dp"/>	
		
	<Button
		android:text="@string/button_open_wtd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_open_wtd"
        android:layout_below="@+id/control_wtd"
		android:layout_marginTop="30dp" 
		android:layout_alignLeft="@+id/button_set_timeoff"/>
		
	<Button
		android:text="@string/button_close_wtd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_close_wtd"
        android:layout_below="@+id/control_wtd"
		android:layout_marginStart="955dp"
		android:layout_marginTop="30dp"/>
		
	<Button
		android:text="@string/button_clean_wtd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_clean_wtd"
        android:layout_below="@+id/control_wtd"
		android:layout_marginTop="30dp" 
		android:layout_alignRight="@+id/button_cancel_timeoff"/>
		
</RelativeLayout>

(1) :RelativeLayout
RelativeLayout 是一种相对布局,控件的位置是按照相对位置来计算的,后一个控件在什么位置依赖于前一个控件的基本位置,是布局最常用,也是最灵活的一种布局。

(2): <RelativeLayout :表示采用相对布局管理器。

(3) :
android:layout_width=“match_parent” 表示布局管理器宽度和高充将填充整个屏幕宽度和高度。
android:layout_height=“match_parent” 表示布局管理器宽度和高充将填充整个屏幕宽度和高度

(4) :
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingBottom : 是指控件中内容距离控件底边距离,表明在 我的 values 文件夹下面的 dimens 文件里面有一个name叫做activity_vertical_margin的项,这个项里面值就是你android:paddingBottom的值 如 :16dp

android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingLeft : 是指控件中内容距离控件左边距离,表明在 我的 values 文件夹下面的 dimens 文件里面有一个name叫做activity_horizontal_margin的项,这个项里面值就是你android:paddingLeft的值 如 :16dp

android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingRight : 是指控件中内容距离控件右边距离,表明在 我的 values 文件夹下面的 dimens 文件里面有一个name叫做activity_horizontal_margin的项,这个项里面值就是你android:paddingLeft的值 如 :16dp

android:paddingTop="@dimen/activity_vertical_margin"
android:paddingTop : 是指控件中内容距离控件上边距离,表明在 我的 values 文件夹下面的 dimens 文件里面有一个name叫做activity_vertical_margin的项,这个项里面值就是你android:paddingBottom的值 如 :16dp

就是从 dimens.xml 文件中获取 activity_vertical_margin、 activity_horizontal_margin、 activity_horizontal_margin、 activity_vertical_margin

(5):
tools:context=“mhr.video.com.alarmshutdown.AlarmActivity”>
一般在根布局文件中会出现 tools:context = 某个activity名称。 这个属性通常在一个布局XML文件的根元素中设置,记录了这个布局关联到哪一个activity(因为显然一个布局在设计时可以被多个布局使用)

(6) : android:id="@+id/activity_toolbox" 文件ID

(7) :

   <TextView   //表示该控件是  UI 上面的文本,一般代表某个控件的名称
   
     // 该控件上面显示的文字 ,该string/starttime存储在 res\values\strings.xml 中,如:<string name="starttime">定时开机</string>
	        android:text="@string/starttime" 

	        android:layout_width="wrap_content" //表示宽度匹配内容
	        android:layout_height="wrap_content" //表示高度匹配内容
	        android:id="@+id/textView_time_on"  /该控件ID,其他控件想要以该控件为标准按照某种方向摆放的时候就引用该ID
		    
		    //该控件的位置
		    android:layout_marginStart="170dp" 
	        android:layout_alignParentTop="true"/>
	        
--------


	<Button  //表示该控件是一个按键
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_cancel_timeon"

//表示该按钮的名称,文字显示在按钮中央,该string/canle 存储在 res\values\strings.xml 中,如: <string name="canle">取消</string>
        android:text="@string/canle" 
        
        //该控件位置
        android:layout_below="@+id/timePicker_on"
		android:layout_alignRight = "@+id/timePicker_on"/>

---------

<DatePicker //表示 日期选择器控件
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
	android:calendarViewShown="false" //这里有句代码控制是否显示日历视图
    android:id="@+id/datePicker_on"
	android:layout_below="@+id/textView_time_on"
	android:layout_marginStart = "5dp"/>
	
------------

<TimePicker //表示时间选择器控件
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/timePicker_on"
    android:layout_below="@+id/textView_time_on"
	android:layout_toRightOf = "@+id/datePicker_on" />

在android的UI布局中,遇到了这样的一个问题,在LinearLayout中放置的控件(如:button),使用android:layout_marginRight="10dp"想让控件距离右边边框10dp,但是实际效果却是靠近左边,设置20dp 30dp也没有起作用,而换成android:layout_marginLeft="10dp"则起作用。引起这种问题的原因是,控件默认左对齐,android:layout_marginLeft=10dp是以左边框为基准的,控件会放置在离左边框至少10dp的位置。如果这个时候设置android:layout_marginRight=“10dp”,它的意思是,距离左边边框至少是10dp(还是以左边为准,顺序排列,只要符合距离有边框至少10dp就行了),那么怎么样实现空间靠右边放置呢?
使用RelativeLayout,在控件属性中使用android:layout_alignParentRight=“true”,这个时候控件已经是以父控件的右边为基准靠右放置,这个时候再设置android:layout_marginRight=“10dp”,就是以右边为基准,距离右边10dp了。

2 value目录

在这里插入图片描述

string.xml 中字符串和数值
arrays.xml 定义string数组
colors.xml 定义颜色
dimens.xml 定义尺寸
styles.xml 定义样式
2.1 string.xml

为什么需要把应用中出现的文字单独存放在string.xml文中呢?

一:是为了国际化,Android建议将在屏幕上显示的文字定义在strings.xml中,如果今后需要进行国际化,比如我们开发的应用本来是面向国内用户的,当然在屏幕上使用中文,而如今我们要让应用走向世界,打入日本市场,当然需要在手机屏幕上显示日语,如果没有把文字信息定义在string.xml中,就需要修改程序的内容了。但当我们把所有屏幕上出现的文字信息都集中存放在string.xml文件之后,只需要再提供一个string.xml文件,把里面的汉字信息都修改为日语,再运行程序时,android操作系统会根据用户手机的语言环境和国家来自动选择相应的string.xml文件,这时手机界面就会显示出日语。这样做国际化非常的方便。

二:为了减少应用的体积,降低数据的冗余。假设在应用中要使用"我们一直在努力"这段文字1000次,如果我们不将"我们一直在努力"定义在string.xml文件中,而是在每次使用时直接写上这几个字,这样下来程序中将有70000个字,这70000个字占136KB的空间。

如下该string.xml内存存放着 APP控件的字符串信息。

<resources>
    <string name="app_name">系统接口</string>
    <!--  textview  !-->
	
    <string name="shutdowntime">定时关机</string>
    <string name="starttime">定时开机</string>
	<string name="Remote_OnOff_LCD">远程开关屏</string>
	<string name="control_wtd">看门狗控制</string>
	<string name="button_open_wtd">打开看门狗</string>
	<string name="button_close_wtd">关闭看门狗</string>
	<string name="button_clean_wtd">清除看门狗</string>
    <string name="canle">取消</string>
    <string name="set">设置</string>
    <string name="enable_backlight">远程开屏</string>
    <string name="disable_backlight">远程关屏</string>
</resources>

例1:在程序中获取string.xml中字符串和数值

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hello">Hello World, MainActivity!</string> 
    <string name="app_name">TestExample01</string> 
</resources> 

在Activity中使用:

String appName=(String) this.getResources().getText(R.string.app_name); 
//或者 String appName=(String) this.getResources().getString(R.string.app_name); 
Log.i("test", "appName="+appName); 
2.2 colors.xml

定义颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

例1

colors.xml
    <?xml version="1.0" encoding="utf-8"?> 
    <resources> 
        <color name="black">#FFFFFF</color> 
    </resources> 

  
调用:
---getResources().getDrawable(R.string.black); 
---getResources().getColor(R.string.black); 
2.3 dimens.xml

定义尺寸

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>

例1

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
   <dimen name="height">80dip</dimen> 
</resources> 

调用     
---getResource().getDimension(R.string.height); 
2.4 styles.xml

定义样式

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>
</resources>

三 src目录

本app src目录下只有一个MainActivity.java文件用来监控处理APP界面控件消息。
src/com/xxx0/xxx1/MainActivity.java

package com.xxx0.xxx1; //包名
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;

public class MainActivity extends Activity {
private static final String TAG = "testapp";

 protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        
	Button button_open_lcd = (Button) findViewById(R.id.button_open_lcd);  //绑定layout xml文件中的button_open_lcd ID
    Button button_close_lcd = (Button) findViewById(R.id.button_close_lcd);   //绑定layout xml文件中的button_close_lcd ID
	button_open_lcd.setOnClickListener(button_listencer_open_lcd); //监听控件
	button_close_lcd.setOnClickListener(button_listencer_close_lcd);//监听控件
    
 }
 
	//开背光
	public Button.OnClickListener button_listencer_open_lcd = new Button.OnClickListener(){

        @Override
        public void onClick(View v) {
		...
        }
    };
	
	//关背光
	public Button.OnClickListener button_listencer_close_lcd = new Button.OnClickListener(){

        @Override
        public void onClick(View v) {
		...
        }
    };

}

猜你喜欢

转载自blog.csdn.net/LinuxArmbiggod/article/details/85253316