本篇博客主要是讲的AndroID开发,学习目的是为了大疆的MSDK开发做好准备。视频链接如下:https://www.bilibili.com/vIDeo/BV1sK411s7Vp
2021/02/241.AndroID项目的开发过程
接下来就是修改这两个文件里面的内容,在这里就要求读者掌握xml编写的基础知识和Java的基础知识修改完毕后就可以查看结果了
这样就可以较为简便地多次使用重复字符串了。
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 androID:text="hello"/>
效果就如下所示:
以上是内存溢出为你收集整理的Android开发学习(1)全部内容,希望文章能够帮你解决Android开发学习(1)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)