Android开发学习(1)

本篇博客主要是讲的Android开发,学习目的是为了大疆的MSDK开发做好准备。视频链接如下:https://www.bilibili.com/video/BV1sK411s7Vp

2021/02/24

1.Android项目的开发过程
在这里的第一步我们通常用AS来替代
在这里的第一步,初学者建议用软件AS来实现,配置较为简单。第二步和第三步的目的如下图所示:
在这里插入图片描述
在安卓应用程序中,逻辑控制层与表现层是分割开来的。逻辑控制层由Java应用程序实现,表现层由xml文档实现。下面将详细介绍如何创建一个项目即APP,并且用AVD进行仿真实现。

  • 新建一个 Empty Activity
  • 可以得到.java后缀的和.xml后缀的两个文件,他们就是上述提到的表现层和逻辑控制层
    在这里插入图片描述
  • 接下来就是修改这两个文件里面的内容,在这里就要求读者掌握xml编写的基础知识和Java的基础知识
  • 修改完毕后就可以查看结果了
    在这里插入图片描述
    右2图标可以选择安卓手机的型号,选定后就可以用左1和左2两个图标开始测试。
  • 在编写xml文件时值得注意的是:如果出现字符串可以把字符串放到res-values-string.xml的路径下,方便以后的调用,意思即如下,其中上图为string.xml,下图为activity.xml文件:
    在这里插入图片描述在这里插入图片描述
    这样就可以较为简便地多次使用重复字符串了。

2.Android的调试
在这里,我们以一个按钮作为一个例子加以讲解。首先在xml文件中给button加一个单击方法:

<EditText
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/message"   --提示文字
        tools:layout_editor_absoluteX="6dp"
        tools:layout_editor_absoluteY="46dp"></EditText>
<Button
        android:id="@+id/btn"  --按钮名称
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"     --按钮上的文字显示
        android:onClick="sendMessage"/>    --按钮单击时的方法

接着在.java文件中给sendMessage方法添加效果

package com.dji.mainactivity;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("MainActivityMSG","iMessage"); //info
        Log.d("MainActivityMSG","dMessage"); //debug
        Log.e("MainActivityMSG","eMessage"); //error
        Log.w("MainActivityMSG","wMessage"); //warning
    }

public void sendMessage(View view) {
        //响应按钮的事件 点击按钮 即可输出编辑框内的内容
        EditText msg=findViewById(R.id.message);
        String s=msg.getText().toString();
        // System.out.println(s);
        //log.i(tag:,msg:)
        Log.i("message",s); //一般用这个进行调试
    }

想要迅速找到具体信息,只需点击如下所示的图标 6:Logcat 就可以了,调试一般用的是log.进行输出:
在这里插入图片描述
断点:单击代码行即可,其意图就是让程式执行到断点处停止,但是注意此时应该用 debug 这种调试方式。

3.Android项目文档结构
在这里插入图片描述
一般使用android模式,package模式。此外,若要调整字号大小,则file-setting-font,即可进行调整。

4. 程式跳转
首先在同一个package下添加一个Empty activity起名为secondActivity,修改xml文件形成新的界面。再打开manifest查看信息,修改信息,即把secondActivity内加入和mainActivity内相同的内容,如下所示:

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

<activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

然后再打开mainActivity.java。再按钮单击的方法sendMessage中加入跳转函数。如下所示:

public void sendMessage(View view) {
        // 显示启动,写法1:class跳转
        Intent intent=new Intent(this,SecondActivity.class);
        this.startActivity(intent);
        //显示启动,写法2:包名+类名
        Intent intent1=new Intent();
        intent1.setClassName(this,"com.dji.mainactivity.SecondActivity");
        startActivity(intent1);
        //显示启动,写法3:ComponentName
        Intent intent2=new Intent();
        ComponentName cname=new ComponentName(this,SecondActivity.class);
        intent2.setComponent(cname);
        startActivity(intent2);
        //隐式启动,写法1
        Intent intent3=new Intent();
        intent3.setAction("action.next");
        startActivity(intent3);
        //隐式启动,写法2
        Intent intent4=new Intent("action.next");
        startActivity(intent4);
    }

manifest 的SecondActivity中如果直接复制mainActivity的代码,则会再手机上出现两个安卓程序,解决方法就是,把category的.LAUNCH改成.DEFAULT即可完成,如下所示:

 <category android:name="android.intent.category.LAUNCHER" />
 改为:
  <category android:name="android.intent.category.DEFAULT" />

manifest中的东西除了一个category还有一个action,修改manifest如下,接着参考上述隐式启动即可实现。值得注意的是:当多个activity的action的名字相同时,跳转会发生冲突,将由用户决定跳转哪一个程式。

 <action android:name="android.intent.action.MAIN" />
 改为
  <action android:name="action.next" />

5.Activity的生命周期

  • onCreate() 建立
  • onStart() 启动
  • onResume() 恢复
  • onPause() 暂停
  • onStop() 停止
  • onDestry() 销毁
  • onRestart() 重启

用示意图可以如下表示:
在这里插入图片描述
当出现两个activity的时候,则生命周期如下所示:
在这里插入图片描述
6. Android的布局

UI设计相关的一些概念

  • view 可以被理解为视图,占据屏幕上的一块矩形区域,负责提供组件绘制和事件处理的方法;是所有的widgets组件的基类;在android.view包中;view的子类一般都位于android.widget的包中
  • view类支持的常用xml属性及对应的方法
    在这里插入图片描述
  • viewGroup 可以被理解成容器,继承自view类,是view类的扩展;其是一个抽象类,在实际应用中使用viewGroup的子类来作为容器的。其子类有LinearLayout,FrameLayout, RelativeLayout, TableLayout, AbosoluteLayout。
    在这里插入图片描述
  • 参数可以被理解为下图所示:
    在这里插入图片描述
  • Activity 代表的是显示给用户的窗口或者屏幕,安卓定义Activity使用一个view和viewgroup的树状节点; 要显示一个用户界面就需要给一个Activity分配一个view或者布局
  • 布局组件 LinearLayout,FrameLayout, RelativeLayout, TableLayout, AbosoluteLayout
    (1). LinearLayout代码
<LinearLayout android:layout_height="200dp"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:gravity="center"  --元素在布局中的位置
    android:layout_gravity="center" --布局在整个activity的位置
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮4"
        />
</LinearLayout>

2021/02/25

(2). FrameLayout

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#0f0"/>

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#fff"
        android:text="重庆大学"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_gravity="right"/>
</FrameLayout>

在这里插入图片描述
由此可见,FrameLayout是一层叠一层的显示

补充知识:Android中的单位,如下所示:

  • px(像素):屏幕中的点,不同设备显示效果相同;
  • in(英寸):长度单位;
  • pt(榜):1/72英寸;
  • dp(与密度无关的像素):一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp=1px;
  • dip:与dp相同,device independent pixels(设备独立像素)。不同设备有不同的显示效果,多用于google设备中;
  • sp(与刻度无关的像素):与dp类似,但是可以根据用户的字体大小首选项进行缩放,scaled pixels(放大像素)。主要用于字体显示best textsize。
  • 尽量使用dp为空间大小单位,sp为字体大小单位
    (3). RelativeLayout
    在这里插入图片描述
    在这里插入图片描述
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <TextView
        android:id="@+id/id1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="相对布局"
        android:textSize="40sp"/>
    
    <EditText
        android:id="@+id/id2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/id1"
        android:background="#0f0"/>
    
    <Button
        android:id="@+id/id3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        android:layout_alignParentRight="true"
        android:layout_below="@id/id2"
        android:layout_marginRight="10dp"
        android:layout_marginTop="1dp"/>

    <Button
        android:id="@+id/id4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="quit"
        android:layout_below="@id/id2"
        android:layout_toLeftOf="@id/id3"
        android:layout_marginRight="50dp"/>
        
</RelativeLayout>

在这里插入图片描述
主要是相对位置和margin的设置。

(4). TableLayout

<?xml version="1.0" encoding="utf-8"?>

<TableLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:shrinkColumns="0"  --压缩TableRow第一个元素
    android:stretchColumns="1" --拉伸TableRow第二个元素
    android:collapseColumns="" --隐藏TableRow的某个元素
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="按钮1"/>

    <TableRow>  --让按钮在同一行
        <Button android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="按钮2"/>
        <Button android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="按钮3"/>
        <Button android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="按钮4"/>
        <Button android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="按钮5"/>
    </TableRow>
    
</TableLayout>

在这里插入图片描述
(5). GridLayout 网格布局

与表格布局相似,但是要比表格布局更加灵活

<?xml version="1.0" encoding="utf-8"?>

<GridLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:rowCount="6"
    android:columnCount="4"  --6行4列的表格
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="serch"
        android:textSize="30sp"
        android:layout_columnSpan="4"/>  --占据四列
    
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="delete"
        android:layout_columnSpan="4"/>
    
    <Button android:text="1" android:textSize="25sp"/>
    <Button android:text="2" android:textSize="25sp"/>
    <Button android:text="3" android:textSize="25sp"/>
    <Button android:text="+" android:textSize="25sp"/>
    <Button android:text="4" android:textSize="25sp"/>
    <Button android:text="5" android:textSize="25sp"/>
    <Button android:text="6" android:textSize="25sp"/>
    <Button android:text="-" android:textSize="25sp"/>
    <Button android:text="7" android:textSize="25sp"/>
    <Button android:text="8" android:textSize="25sp"/>
    <Button android:text="9" android:textSize="25sp"/>
    <Button android:text="*" android:textSize="25sp"/>
    <Button android:text="." android:textSize="25sp"/>
    <Button android:text="0" android:textSize="25sp"/>
    <Button android:text="=" android:textSize="25sp"/>
    <Button android:text="/" android:textSize="25sp"/>

在这里插入图片描述
(6). 外部布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:layout_width="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        android:textSize="20dp"/>

    </LinearLayout>
 <include layout="@layout/activity_third"></include>

在这里插入图片描述
7. 样式与主题

样式:是用于指定view或window的外观和格式的一系列属性的集合,主要包括height,width,padding,color,textsize,background。

主题:针对于一个activity,其中所有的view都可以使用这一种主题

首先,需要在res-values-color.xml中,为相应的颜色设置代号以便使用,如下列的代码所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="color1">#EFEFEF</color>
    <color name="color2">#3E4F3E</color>
    <color name="color3">#007f0e</color>
</resources>

接下来,在res-value-style.xml中添加代码,设置主题,如下所示:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme2" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/color1</item>
        <item name="colorPrimaryDark">@color/color2</item>
        <item name="colorAccent">@color/color3</item>
    </style>
</resources>

最后在 manifest中选择需要的主题,如下所示:

android:theme="@style/AppTheme">

同样地,我们可以在style.xml中设置 样式

<resources>
    <style name="myFont" parent="TextAppearance.AppCompat.Medium">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">#F00</item> --红色
    </style>
</resources>

然后再一个view中(譬如在一个textView)加入样式style就可以了

 <TextView
        style="@style/myFont"
        android:text="hello"/>

效果就如下所示:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值