Android基础学习【历史流程重走】 ---- Android入门基础(一)

来源:互联网 发布:网络舆论战 编辑:程序博客网 时间:2024/03/29 22:19

一、历史回顾

随科技的迅速发展,当前已经全线进入4G时代,5G时代也即将开启。Android版本迭代迅速,如今已是6.0的版本。时不时可以看到,手机危害了当前人群的生活,如熬夜看手机会深度伤害眼睛,上班族路上低头党,聚会吃饭外只剩手机党等。确实存在一些情形,但是却无不在强调在当今社会手机在人的生活中,占有越来越重的地位。手机通讯,到手机娱乐、办公,以至于到手机管理自身财富。甚至于将来,手机将管理我们的车、房。技术的潮流不可阻挡,最好是做一个弄潮儿,次之可以“随波逐流”。聊了很多题外话,就是想说,做移动开发的小伙伴,你们太有眼光了!

重走Android重生路,一切才刚刚开始~_~


二、Android体系结构、虚拟机


Kernel作为内核,驱动硬件,驱动硬件实现最终目的效果;对内核的驱动操作,封装成为库文件,形成Library;Application就是一个应用,系统应用有打电话、发短信、照相等,实际的开发应用有工具、商城、游戏类等;Application Framework构成针对Android应用的顶层管理。包括Activity Manager,Window Manager 等。

相比于JVM,Dalvik虚拟机有更多的优势,将所有头部组合,抽离出来常量池,将方法分门别类,实现所有java的整理。理解帮助:将一个文件压缩成为压缩包后拷贝,提升数据传输速度。


之后又推出ART虚拟机。java作为高级语言,在机器执行命令之前需要编译。开启ART虚拟机,在程序安装时就直接将程序翻译成为机器语言。从而实现代码命令直接执行,从而提升效率。但是占用内存大、运行耗能多、内部依旧不稳定,阻碍了ART的快速扩展。


三、Android开发环境

ADT是使用率最高的环境。最近,随着Android Studio的逐渐成熟,ADT的使用,正在逐渐被替换掉。Google公司不提供后续维护,是最大的原因。如下是Eclipse环境下的项目目录结构:


在AS下使用Project模式,项目目录结构相似。AS更大的优势在于,内部嵌套gradle,能够实现自动打包、多版本、多渠道打包。

ADB即是Android Debug Bridge,用于连接开发环境和运行环境。adb命令可以方便使用。常用adb命令:

<span style="font-size:18px;">adb  kill-server :杀死服务,断开连接adb  start-server :开启服务,连接设备                                                       【adb install XXX.apk :安装手机软件   安装不可用】adb  devices  :重启服务,链接设备adb  connect 127.0.0.1:6555  链接天天模拟器  adb  connect 127.0.0.1:62001  链接夜神模拟器adb uninstall  包名:卸载手机软件   卸载可用  Adb shell  进入设备Adb shell input keyevent BACK按键Adb shell input tap X Y 点击坐标点Adb shell input swipe X Y X Y滑动adb shell dumpsys activity  [ activities ]   查看activity   [] 可选adb    ps 是看进程的adb    top命令是看占用率的     查看手机CPU占用率                 7817  0  15% R    37 596756K  72764K  fg u0_a75   com.ds365.order.test  //Monkey运行过程中                 8599  0   3% S     34 586604K  56884K  fg u0_a75    com.ds365.order.test   //运行                 8599  0   0% S     30 573724K  55252K  bg u0_a75   com.ds365.order.test   //后台运行</span>
在开发环境中,还提供了一些工具,如DDMS,hierarchyviewer。

DDMS可以管理虚拟机。

hierarchyviewer可用于查找View id,弄清楚View之间的相互关系。当前工具对于MonkeyRunner自动化测试提供很好的帮助。


四、一个小程序

<span style="font-size:18px;"><span style="font-size:18px;">public class MainActivity extends AppCompatActivity {    private Button phoneCall;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        phoneCall = (Button) findViewById(R.id.phone_call);        phoneCall.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                /**                 * 测试发送消息                 */                SmsManager smsManager = SmsManager.getDefault();                smsManager.sendTextMessage("15510728213", "moniqi", "你好", null, null);                /**                 * 指定意图:新建对象,设置动作,携带数据,触发动作                 */                Intent intent = new Intent();                intent.setAction(Intent.ACTION_CALL);                intent.setData(Uri.parse("tel://15510728213"));                startActivity(intent);            }        });    }    /**     * 跳转下一界面     *     * @param view     */    public void changeNextPage(View view) {        startActivity(new Intent(MainActivity.this,OnClickEventActivity.class));    }}</span></span>
基本编程流程:修改编写xml文件,写主类:找到关心控件,为控件添加事件。
点击事件的四种写法:

<span style="font-size:18px;">public class OnClickEventActivity extends Activity implements View.OnClickListener {    private Button clickSecond;    private Button clickThird;    private Button clickFour;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.onclick_event_layout);        clickSecond = (Button) findViewById(R.id.onclick_second);        clickThird = (Button) findViewById(R.id.onclick_third);        clickFour = (Button) findViewById(R.id.onclick_four);        clickSecond.setOnClickListener(new ClickSecond());        clickThird.setOnClickListener(new View.OnClickListener() {            /**             * 第三种方法:匿名内部类实现点击方法             */            @Override            public void onClick(View v) {                Toast.makeText(OnClickEventActivity.this, "匿名内部类实现", Toast.LENGTH_SHORT).show();            }        });        clickFour.setOnClickListener(this);    }    /**     * 第一种方式:xml中写方法     *     * @param view     */    public void clickEventFirst(View view) {        Toast.makeText(OnClickEventActivity.this, "xml中写方法", Toast.LENGTH_SHORT).show();    }    /**     * 第四种方法:类实现Onclick接口     */    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.onclick_four:                Toast.makeText(OnClickEventActivity.this, "类实现Onclick接口", Toast.LENGTH_SHORT).show();                break;        }    }    /**     * 第二种方法:内部类实现点击方法     */    class ClickSecond implements View.OnClickListener {        @Override        public void onClick(View v) {            Toast.makeText(OnClickEventActivity.this, "内部类实现", Toast.LENGTH_SHORT).show();        }    }}</span>

五、四大布局

四大布局是:RelativeLayout、LinearLayout、FrameLayout、TableLayout。绝对布局不建议使用。

相对布局(RelativeLayout):

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/middle"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="中间" />    <Button        android:id="@+id/left"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_toLeftOf="@+id/middle"        android:text="左" />    <Button        android:id="@+id/right"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_toRightOf="@+id/middle"        android:text="右" />    <Button        android:id="@+id/up"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@+id/middle"        android:layout_centerHorizontal="true"        android:text="上" />    <Button        android:id="@+id/down"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/middle"        android:layout_centerHorizontal="true"        android:text="下" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="左上" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:text="右上" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:text="左下" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:text="右下" /></RelativeLayout></span>


线性布局(LinearLayout):

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:text="LinearLayout第一行第一列" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:text="LinearLayout第一行第二列" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:layout_weight="1"            android:text="LinearLayout第二行第一列" />        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:layout_weight="1"            android:text="LinearLayout第二行第二列" />    </LinearLayout></LinearLayout></span>

帧布局(FrameLayout):

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="350dp"        android:layout_height="350dp"        android:background="@color/black"        android:layout_gravity="center"/>    <TextView        android:layout_width="300dp"        android:layout_height="300dp"        android:background="@color/blue"        android:layout_gravity="center"/>    <TextView        android:layout_width="260dp"        android:layout_height="260dp"        android:background="@color/red"        android:layout_gravity="center"/>    <TextView        android:layout_width="200dp"        android:layout_height="200dp"        android:background="@color/green"        android:layout_gravity="center"/>    <TextView        android:layout_width="100dp"        android:layout_height="100dp"        android:background="@color/colorAccent"        android:layout_gravity="center"/></FrameLayout></span>

RelativeLayout属性说明:

   第一类:属性值为true或false 

    android:layout_centerHrizontal  水平居中 
    android:layout_centerVertical   垂直居中 
    android:layout_centerInparent    相对于父元素完全居中 
    android:layout_alignParentBottom 贴紧父元素的下边缘 
    android:layout_alignParentLeft   贴紧父元素的左边缘 
    android:layout_alignParentRight  贴紧父元素的右边缘 
    android:layout_alignParentTop    贴紧父元素的上边缘 
    android:layout_alignWithParentIfMissing  如果对应的兄弟元素找不到的话就以父元素做参照物 

    第二类:属性值必须为id的引用名“@id/id-name” 
    android:layout_below      在某元素的下方 
    android:layout_above      在某元素的的上方 
    android:layout_toLeftOf   在某元素的左边 
    android:layout_toRightOf  在某元素的右边 

    android:layout_alignTop   本元素的上边缘和某元素的的上边缘对齐 
    android:layout_alignLeft  本元素的左边缘和某元素的的左边缘对齐 
    android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐 
    android:layout_alignRight  本元素的右边缘和某元素的的右边缘对齐 

    第三类:属性值为具体的像素值,如30dip,40px 
    android:layout_marginBottom              离某元素底边缘的距离 
    android:layout_marginLeft                   离某元素左边缘的距离 
    android:layout_marginRight                 离某元素右边缘的距离 
    android:layout_marginTop                   离某元素上边缘的距离 

android:gravity  
android:gravity属性是对该view 内容的限定.比如一个button 上面的text.  你可以设置该text 在view的靠左,靠右等位置.以button为例,android:gravity="right"则button上面的文字靠右 


android:layout_gravity 
android:layout_gravity是用来设置该view相对与起父view 的位置.比如一个button 在linearlayout里,你想把该button放在靠左、靠右等位置就可以通过该属性设置.以button为例,android:layout_gravity="right"则button靠右 


EditText 的 android:hint 
设置EditText为空时输入框内的提示信息。 


ImageView 的 android:scaleType: 
android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType / android:scaleType值的意义区别: 


CENTER /center  按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示 
CENTER_CROP / centerCrop  按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽) 
CENTER_INSIDE / centerInside  将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽 
FIT_CENTER / fitCenter  把图片按比例扩大/缩小到View的宽度,居中显示 
FIT_END / fitEnd   把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置 
FIT_START / fitStart  把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置 
FIT_XY / fitXY  把图片不按比例扩大/缩小到View的大小显示 
MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。 

源码下载

雄关漫道真如铁 而今迈步从头越~_~        

0 0