走进安卓的世界
manifests–保存配置文件
java–保存Java源代码文件
res–保存资源文件(如图片,布局文件,菜单资源,字符串等)
Gradle Scripts–保存Gradle构造和属性文件
AndroidManifest.xml文件元素和说明
manifest —— 根节点,描述package中所有内容
xmlns:andriod —— 包含命名空间的声明
package —— 声明应用程序包
application —— 包含package中application级别组件声明的根节点
一个manifest可以包含0个或1个该元素
android:icon —— 应用程序图标
:label —— 应用程序名称
:theme —— 应用采用的主题
activity —— 与用户交互的主要工具,它是用户打开一个应用的初始页面
intent-filter —— 配置Intent过滤器
action —— 组件支持的Intent Action
category —— 组件支持的InetentCategory启动的页面
res节点
drawable —— 保存图片资源文件(png,jpg,gif,9-patch或shape)
layout —— 布局文件
mipmap —— 应用启动图标
xxxhdpi(超超超高清)
xxhdpi(超超高清)
xhdpi(超高清)
hdpi(高清)
mdpi(中)
values —— 字符串,样式或颜色资源
用户界面设计基础
View(视图)
View类支持的常用xml属性和对应的方法
android:background SetBackgroundResource(int)//设置背景
:clickable SetClickable(boolean)//设置是否响应单击事件
:elevation SetElevation(float)//设置z轴深度
:id SetId(int)//设置组建的id
:longClickable SetLongClickable(boolean)//设置是否长按事件
:minHeight SetMinimumHeight(int)//设置最小高度
:minWidth SetMinimumWidth(int)//设置最小宽度
:onClick 无//设置单击事件触发的方法
:padding SetPaddingRelative(int,int,int,int)//设置四个边的内边距
:paddingBottom SetpaddingBottom(int,int,int,int)//设置底边的内边距
:paddingTop //同上
:paddingStart //同上
:paddingEnd //同上
:paddingLeft //同上
:paddingRight //同上
ViewGroup(容器)
常用xml属性
android:layout_marinBottom //设置底外边距
:layout_marinTop //设置顶外边距
:layout_marinEnd //设置右外边距
:layout_marinStart //设置左外边距
:layout_marinLeft //设置左外边距
:layout_marinRight //设置右外边距
布局管理器
- 相对布局(RelativeLayout)
- 线性布局(LinearLayout)
- 帧布局(FrameLayout)
- 表格布局(TableLayout)
- 绝对布局(AbsoluteLayout)
相对布局(RelativeLayout
常用xml属性
android:gravity —— 用于设置布局管理器中各自组建的对齐方式
:ignoreGravity —— 用于指定那个组件不受gravity的影响
RelativeLayout.layoutParams支持的常用xml组件
android:layout_above 该组件位于那个组件上方
:layout_below 该组件位于那个组件下方
:layout_alignBottom 指定该组件与那个组件的下边界对齐
_alignTop 指定该组件与那个组件的上边界对齐
_alignLeft 指定该组件与那个组件的左边界对齐
_alignRight 指定该组件与那个组件的右边界对齐
:layout_alignParentBottom 指定该组件是否与布局管理器的底端对齐
:layout_alignParentLeft .........................的左端对齐
...................Right .........................的右端对齐
...................Top .........................的顶端对齐
android:layout_centerHorizontal 指定该组件位于布局管理器水平居中的位置
..............Inparent .......................中央居中的位置
..............Vertical .......................垂直居中的位置
android:layout_toLeftof 指定该组件位于那个组件的左侧
...............toRightof 指定该组件位于那个组建的右侧
线性布局(LinearLayout)
常用xml属性
android:orinentaction 用于设置布局管理器内组件的排序方式
//可以选择horizeontal或者vertical
android:gravity —— 用于设置布局管理器中各自组建的显示位置
子组件在LinearLayout支持的常用xml组件
android:gravity 用于设置组件在其父容器的位置
android:weight 用于设置组件所占的权重
网格布局(gridLayout)
帧布局(FrameLayout)
表格布局(TableLayout)
常用UI组件
文本类组件
文本框(TextView)
xml属性
android:autoLink //用于指定是否将指定格式的文本转换为可单击的超链接形式,属性值有:none,web,email,phone,map,all
android:drawableBottom 在文本框的文本的底部绘制指定图片
:drawableLeft 在文本框的文本的左侧绘制指定图片
:drawableRight 在文本框的文本的右侧绘制指定图片
:drawableTop 在文本框的文本的顶部绘制指定图片
:drawableStart 在文本框的文本的左侧绘制指定图片
:drawableEnd 在文本框的文本的右侧绘制指定图片
android:gravity 设置文本框文本的对齐方式
android:hint 设置当文本框内容为空时,显示的文字
android:inputType 指定当前文本框显示内容的文本类型
android:singleLine 指定该文本框是否为单行模式
android:text 指定该文本框中显示的文字内容
android:textColor 指定文本颜色
android:textSize 指定文本字体大小
android:width 宽
android:height 高
编辑框(EditText)
xml属性
如果是密码框 可以把inputType设置为password属性
按钮类组件
普通按钮(Button) 是Textview的子类
xml属性
android:Background="" 按钮背景
图片按钮(ImageButton)
xml属性
android:src="" 按钮的图片
android:scaleType="" 指定图片的缩放方式
scaleType的属性说明
matrix 使用matrix方式进行缩放
fixXY 对图片横向,纵向独立缩放。让该图片完全适应于ImageButton
fitStart 保持纵横比缩放图片,缩放后在按钮的左上角
fitCenter 保持纵横比缩放图片,缩放后在按钮的中间
fitEnd 保持纵横比缩放图片,缩放后在按钮的左上角
center 放在按钮的中间。但是不进行缩放
centerCrop 保持纵横比缩放图片,使图片完全覆盖
centerInside 保持纵横比缩放,使按钮完全显示图片
单选按钮(RadioButton)
常用xml属性
android:checked="" boolean类型,用于指定选中状态
复选框(CheckBox)
事件监听器
xxx.setOnCheckChangeListener
进度条(ProgressBar)
支持的xml属性
android:max 用于设置进度条的最大值
android:progress 用于指定进度条已完成的进度值
android:progressDrawable 用于设置进度条轨道的绘制形式
styte=""
style属性的可选值
?android:attr/progressBarStyleHorizeontal 细的长条进度条
......................BarStyleLarge 大圆形进度条
BarStyleSmall 小圆形进度条
@android:stylr/Widget.ProgressBar.Large 大跳跃 旋转画面的进度条@android:stylr/Widget.ProgressBar.Small 小跳跃 旋转画面的进度条
@android:stylr/Widget.ProgressBar.Horizeontal 粗的长条水平进度条
拖动条(SeekBar)
事件监听器
seekBar.setOnSeekBarChangeListener
星级评分条(RatingBar)
常用xml属性
android:isIndicator 用于指定该评分条是否允许用户改变 true为不允许
android:numStars 指定总共有多少颗星
android:rating 默认的星级
android:stepSize 指定每次最少要改变多少星星
图像类组件
图像视图(Imageview)
xml属性
android:adjustViewBounds 用于设置组件是否调整自己边界来保持所显示图片的长宽比
android:maxHeight 用于设置Imageview组件的最大宽度
android:scaleType 设置缩放位置
android:src 设置图片
android:tint 用于为图片着色
网格视图(GridView) 后期做笔记
列表类组件
下拉列表框(Spinner)
android:entries="" 用于指定列表项 可以指定在xml中,如array资源文件中
也可以用适配器的方式
1.创建一个适配器,通常使用ArrayAdapter,首先创建字符串数组,然后使用
ArrayAdapter类的构造器实例化ArrayAdapter。
ArrayAdapter<String> adapter=new ArrayAdapter(this,android.R.layout.aimple.simple.spinner.item,数组名);
2.将适配器设置下拉样式
adapter.setDropDownViewResource(android.R.layout.sim);
3.将适配器与下拉列表关联
spinner.setAdapter(adapter)
Spinner.getSelectedItem//获取选中的值
列表视图(ListView) 后期做笔记
基本程序单元Activity
Activity的生命周期中的回调方法
onCreate() 在创建Activity时被回调
onStart() 启动Activity时被回调,也就是当Activity变为可见时被回调
onResume() 当Activity由暂时状态恢复为活动状态时调用
onPause() 暂停activity的回调
onRestart() 重新启动Activity时被回调,该方法总是在onStart()方法之后执行
onStop() 停止Activity时被回调
onDestroy() 销毁Activity时被回调
配置,创建,启动和关闭Activity
1.创建Activity
- 先创建一个Activity,一般继承android.app中的Activity类
- 重写需要的回调方法,都需要重写onCreate()方法并且setContentView()方法设置显示页面
2.配置Activity
在中添加标记实现,每个activity对应一个标记
<activity
android:name="实现类"//对应的类
android:label="说明性文字"//指定标签
android:theme="应用的主题"//主题
</activity>
2.启动Activity
- 创建Intent对象,指定想被启动的Activity
Intent intent=new Intent(MainActivity.this,demo.class);
- 调用startActivity()方法启动
3.关闭Activity
使用Activity类提供的finish()方法
多个activity的使用
使用Bundle在Activity之间交换数据
Bundle是一个key-value(键-值)对的组合
- 将要保存的数据保存在Bundel对象中
Bundle bundle=new Bundle();
bundle.putCharSequence("key值",value)
- 通过Intent提供的putExtras()方法,将把携带的数据保存到Intent中
intent.putExtras(bundle);
-
startActivity(intent)
-
另一个类中获取Intent对象
Bundle bundle=intent.getExtras();
- 再把Bundle的值传给别人
比如
String demo=bundle.getString("key值");
调用另一个Activity并返回结果
这个功能需要用到startActivityForResult()方法来启动另一个Activity
格式:
public void startActivityForResult(Intent intent,int requestcode);
requestcode代表启动Activity的请求码
使用Fragment
- 创建Fragment 创建类并继承Fragment
- 在Activity中添加Fragment
使用
<fragment
android:name=""/指定要添加的Fragment
/>
- 当Activity运行时添加Fragment
1.首先实例化有Fragment的类
DemoFragment deFragment=new DemoFragment();
2.获取FragmentTransaction的实例
FragmentTransaction ft=new FragmentTransaction().beninTransaction();
3.添加
ft.add(android.R.id.content,deFragment);
4.提交事务
ft.commit();
Android应用核心Intent
初识Intent
Intent起着一个媒体中介的作用,专门提供组件间互相调用的信息,实现调用者和被调用者之间的解耦
Intent的基本应用
Intent是一个可以从另一个应用程序请求动作的消息处理对象,可以实现组件间的通信。有两种基本应用
1,开启Activity
可以将一个Intent对象传递给startActivity()方法,启动另一个Activity,并且可以携带一些数据
2,开启Service
通过将一个Intent对象传递给startActivity()方法,可以启动一个Service来完成一次操作(如下载文件)或者传递一个新的指令给startActivity()方法。
3,传递Broadcast
显式Intent
显式Intent是指在创建Intent对象时就指定接收者(如activity,Service,BroadcastReceiver)
格式:
Intent intent=new Intent(context.package,context.calss)
隐式Intent
隐式Intent是指在创建Intent对象不指定具体的接收者,而是定义要执行的Action,category,data
例如调用系统游览器打开网页
Intent intent=new Intent();//创建
intent.setAction(Intent.ACTION_VIEW);//设置动作
intent.setDate(url.parse("http://bidu.com"));//设置数据
startActivity(intent);
Intent intent=new Intent(Intent.ACTION_VIEW),url.parse("http://bidu.com");
startActivity(intent);
表
/调用浏览器
Uri uri = Uri.parse("");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
//显示某个坐标在地图上
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.Action_VIEW,uri);
startActivity(it);
//显示路径
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
Intent it = new Intent(Intent.ACTION_VIEW,URI);
startActivity(it);
//拨打电话
Uri uri = Uri.parse("tel:10086");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
Uri uri = Uri.parse("tel.10086");
Intent it =new Intent(Intent.ACTION_CALL,uri);
需要添加 <uses-permission id="android.permission.CALL_PHONE" /> 这个权限到androidmanifest.xml
//发送短信或彩信
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
//发送短信
Uri uri = Uri.parse("smsto:10086");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "cwj");
startActivity(it);
//发送彩信
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra("sms_body", "some text");
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType("image/png");
startActivity(it);
//发送邮件
Uri uri = Uri.parse("mailto:android123@163.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, android123@163.com);
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose Email Client"));
Intent it=new Intent(Intent.ACTION_SEND);
String[] tos={"me@abc.com"};
String[] ccs={"you@abc.com"};
it.putExtra(Intent.EXTRA_EMAIL, tos);
it.putExtra(Intent.EXTRA_CC, ccs);
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it, "Choose Email Client"));
//播放媒体文件
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/cwj.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//卸载APK
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
//卸载apk 2
Uri uninstallUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
//安装APK
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
//使用系统相机
Intent intent = new Intent("android.media.action,IMAGE_CAPTURE");
startActivity(intent);
//播放音乐
Uri playUri = Uri.parse("file:///sdcard/download/sth.mp3");
returnIt = new Intent(Intent.ACTION_VIEW, playUri);
//发送附近
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/cwj.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
//market上某个应用信,pkg_name就是应用的packageName
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//market上某个应用信息,app_id可以通过www网站看下
Uri uri = Uri.parse("market://details?id=app_id");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//调用搜索
Java代码 收藏代码
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,"android123")
startActivity(intent);
Intent过滤器
后期再写