文章目录
- 一. AndroidMainifest.xml文件的结构
- 二. Android的四大组件
- 三. 基础界面组件与布局
- 四. 高级界面组件与布局
- 五. 菜单和对话框
- 六. 服务和消息广播
- 七. 数据的存储与访问
- 八. 多媒体应用开发
- 常用功能类
- 常用资源文件
一. AndroidMainifest.xml文件的结构
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android" <!--定义Android命名空间-->
package="com.example.tong_xun_lu"> <!--指定appJava程序的包名-->
<uses-permission android:name="android.permission.CALL_PHONE"/> <!--权限定义-->
<application
android:icon="@mipmap/ic_launcher" <!--指明App安装后显示的图标-->
android:label="@string/app_name" <!--指明App安装后显示的文字信息-->
android:theme="@style/Theme.AppCompat.Light.NoActionBar"> <!--指明App的主题风格-->
<activity
android:name=".MainActivity" <!--指明activity对应的类名(启动的主文件)-->
android:label="QQ" <!--指明该name指定的Activity运行后的标签名-->
android:screenOrientation="unspecified" <!--横屏竖屏设置-->
android:exported="false" /> <!--拒绝被别的APP启动该Activity-->
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <!--表明此activity是作为App的入口-->
<category android:name="android.intent.category.LAUNCHER" /> <!--决定App是否显示在程序列表里-->
<data android:mimeType="video/mpeq"
android:scheme="http"(URI协议) android:host="www.nnutc.edu.cn"(主机)
android:port="8080"(端口) android:path="/web"(路径)/> <!--用于指定一个URL和数据类型-->
</intent-filter>
</activity>
<activity android:name=".addActivity"></activity> <!--增加addActivity,注意要加点-->
</application>
</manifest>
二. Android的四大组件
(一) Activity(活动)
🔰Activity是Android App的表现层,显示可视化的用户界面,并且接收与用户交互所产生的界面事件;
🔰子类Activity需要实现的方法:
1)onCreate(Bundle)
:初始化Activity。使用setContentView(int)方法将布局资源定义到用户界面上;使用findViewById(int)在用户界面中检索需要编程交互的小部件;
2)onPause()
:处理当离开Activity时要做的事情。
(二) BroadcastReceiver(广播接收器)
🔰BroadcastReceiver是用来接收并相应广播消息的组件,他不含任何用户界面,不做任何事情,仅是接受广播并作出相应的反应;
🔰使用时,可以用Context.registerReceiver()方法在程序代码中动态地注册这个类的使用实例,也可以通过AndroidMainifest.xml中的<receiver>
标签中静态声明;
(三) Service(服务)
🔰Service没有可视化用户界面,但需要长时间在后台运行的应用;
🔰使用时,每个Service类在AndroidMainifest.xml中有相应的<service>
声明;Service可以通过Context.startService()和Context.bindService()启动;
(四) ContentProvider(内容提供者)
🔰 ContentProvider是Android提供的一种标准的共享数据机制,App通过它访问其他App私有数据;
三. 基础界面组件与布局
(一) 组件共用属性
属性值 说明 android:id=“@+id/tiaoshi” ID号,是组件的唯一标识 android:layout_width=“wrap_content” 组件宽度
wrap_content
:大小适应文本内容
match_parent
:大小适应父组件大小android:layout_height=“wrap_content” 组件高度
wrap_content
:大小适应文本内容
match_parent
:大小适应父组件大小android:layout_weight=“5” 设置权重,即让一行或一列的组件按比例显示 android:drawableLeft=“@mipmap/suo”
android:drawableRight
android:drawableTop
android:drawableBottom设置编辑框文本的左边,右边,上边,底部显示的drawable
如:在密码输入框前加个锁android:drawablePadding 设置编辑框文本与drawable的间隔,与drawableLeft等一起使用,可以设置为负数,单独使用没有效果 android:layout_marginLeft=“30dp”
android:layout_marginRight
android:layout_marginTop
android:layout_marginBottom设置编辑框与手机边缘之间的间距 (外边距) android:background=“#369733” 设置Image View的背景颜色或图片
(二) 文本显示组件【TextView】
🔰具备基本的显示文本功能,可设置文本大小,位置,颜色等属性;
效果如下:
属性值 说明 android:text=“hello world!” 显示文本内容 btn.setText("你好");
android:textSize=“20sp” 文本大小,单位sp android:textColor=“#973030” 文本颜色 android:textStyle=“normal” 设置字形
bool 粗体, italic 斜体, normal 正常android:lines=“2” 设置文本的行数 android:autoLink=“web” 设置是否当文本为:URL链接(all, web),E-mail(email),电话号码(phone),Map(map)时,文本显示可单击的链接 android:gravity=“center” 设置文本框内文本的对齐方式
top / bottom / left / right /
center_horizontal(横向中央位置对齐)android:layout_gravity=“center” 组件本身相对于父组件的显示位置
(三) 按钮组件【Button】
🔰Button是TextView的子类,所以TextView上的很多属性可以直接应用到Button上;对于Button操作主要是按下后执行何种操作;>
效果如下:
属性名 说明 android:clickable=“true” 设置是否允许点击按钮 ,true/false btn.setClickable(boolean)
android:background=“#980F0F” 通过资源文件设置颜色 android:onClick 设置点击事件 setOnClickListener(OnClickListener)
1. 按钮点击监听事件
bt = (Button) this.findViewById(R.id.rgWhcd);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 功能代码
}
});
(四) 线性布局【LinearLayout】
🔰可以使用水平(Horizontal) 或 垂直(Vertical) 两种方式放置组件;
属性名 说明 android:orientation=“vertical” 设置线性布局的方向 horizontal(水平)
vertical(垂直)
(五) 编辑框组件【EditText】
🔰EditText是TextView的子类,主要操作是用户与AndroidApp进行数据传输的窗口;
效果如下:
属性名 说明 android:hint=“密码” 设置编辑框内容为空时显示的文本 android:inputType=“phone” 设置编辑框中限制输入的类型:(number—整数,numberDecimal—小数点,date—日期,text—文本,phone—拨号键盘,textPassword—密码,textVisiblePassword—可见密码,textUri—网址) android:maxLength=“10” 限制显示的文本长度,超出部分不显示 android:digits=“123456789” 设置允许输入那些字符
(六) 图像显示组件【ImageView】
🔰用于显示图片,图片可来自资源文件,Drawable对象和ContentProvider;
属性名 说明 android:adjustViewBounds=“true” 设置Image View是否自动调整边界来适应显示图片的宽长比, true/false android:maxHeight=“20dp” 设置Image View的最大高度 android:maxWidth=“20dp” 设置Image View的最大宽度 android:scaleType=“matrix” 设置Image View所显示的图片如何缩放或移动以适应Image View的大小 android:src=“@mipmap/beijing” 设置Image View所显示的图像路径 🔰
src
指ImageView上显示的内容,按图片大小填充,不拉伸;background
指的是背景,会进行拉伸;
(七) 相对布局【RelativeLayout】
🔰它可以在屏幕大小不同,分辨率不同的Android终端屏幕上友好显示的一种布局方式;
1. 设置组件与组件之间的关系和位置的相关属性
属性名 | 说明 |
---|---|
android:layout_above=“@id/shuru” | 将该组件的底部至于给定的ID组件的上面 |
android:layout_below=“@id/shuru” | 将该组件的底部至于给定的ID组件的下面 |
android:layout_toLeftOf=“@id/shuru” | 将该组件的右边缘与给定的ID组件的左边缘对齐 |
android:layout_toRightOf=“@id/shuru” | 将该组件的左边缘与给定的ID组件的右边缘对齐 |
2. 设置组件与组件之间对齐方式的相关属性
属性名 | 说明 |
---|---|
android:layout_alignBaselineabove=“@id/shuru” | 将==该组件的基线与给定ID组件的基线对齐 == |
android:layout_alignTop=“@id/shuru” | 将该组件的顶部与给定ID组件的顶部对齐 |
android:layout_alignBottom=“@id/shuru” | 将该组件的底部与给定ID组件的底部对齐 |
android:layout_alignLeft=“@id/shuru” | 将该组件的左边缘与给定ID组件的左边缘对齐 |
android:layout_alignRight=“@id/shuru” | 将该组件的右边缘与给定ID组件的右边缘对齐 |
3. 设置组件与父组件之间对齐方式的相关属性
属性名 | 说明 |
---|---|
android:layout_alignParentTop=“true” | 将该组件的顶部与父组件的顶部对齐 |
android:layout_alignParentBottom=“true” | 将该组件的底部与父组件的底部对齐 |
android:layout_alignParentLeft=“true” | 将该组件的左边缘与父组件的左边缘对齐 |
android:layout_alignParentRight=“true” | 将该组件的右边缘与父组件的右边缘对齐 |
4. 设置组件方向的相关属性
属性名 | 说明 |
---|---|
android:layout_centerHoriaontal=“true” | 将该组件置于水平方向的中央 |
android:layout_centerVertical=“true” | 将该组件置于垂直方向的中央 |
android:layout_centerInParent=“true” | 将该组件置于水平方向和垂直方向的中央 |
(八) 单选按钮【RadioButton】
🔰在布局中直接定义
RadioButton
组件,如果界面有多个RadioButton
则表示可以多个,或全部选中;
🔰与RadioGroup
配合使用,这种方式定义下的RadioButton
只可以选中一个;
🔰一般RadioButton
组件必须放在RadioGroup
中才能达到效果;
效果如下:
🔰Radio Group的常用属性和方法
属性名/方法名 说明 android:orientation=“horizontal” 设置里面的RadioButton摆布方式;
horizontal(水平)
vertical(垂直)void clearCheck() 清除单选按钮中所有单选按钮的选中状态 int getCheckedRadioButtonId() 返回该单选按钮组中所选按钮的标识id,如果没有勾选则返回-1 void setOnCheckedChangeListener() 注册一个当该按钮组中的单选按钮勾选状态发生改变时所要用的回调函数
1. 单选按钮监听事件
// 三选一,点击那个,就那个显示在标签上
RadioGroup zu = this.findViewById(R.id.zu);
RadioButton nan = this.findViewById(R.id.nan);
RadioButton nv = this.findViewById(R.id.nv);
RadioButton renyao = this.findViewById(R.id.renyao);
TextView wenzi = this.findViewById(R.id.wenzi);
zu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.nan:
wenzi.setText(nan.getText());
break;
case R.id.nv:
wenzi.setText(nv.getText());
break;
case R.id.renyao:
wenzi.setText(renyao.getText());
break;
}}});
(九) 复选框组件【CheckBox]
🔰用于实现多个选项同时被选中的功能,是Button的子类支持Button的所有属性
效果如下:
🔰多选按钮一般都是通过按钮的监听事件来监听。
(十) 下拉列表框【Spinner】
🔰是ViewGroup的间接子类,因此可以作为容器使用;
🔰在value下的strings.xml文件中添加数组作为数据来源;
效果如下:
属性/方法名 说明 android:spinnerMode 列表框的模式
dialog:对话框风格的窗口
dropdown:下拉菜单风格的窗口android:entries=“@array/shuzu” 使用数组资源设置改下拉列表的列表项目 android:dropDownWidth=“200px” 设置下拉列表框的宽度 android:dropDownVerticalOffset=“60px” 设置列表框的水平垂直距离
下拉列表框整体往下移*单位android:prompt=“@string/biaoti” 设置对话框模式列表框的标题,只能引用string.xml中的资源id,不能直接写字符串 android:dropDownSelector=“#F3A0A0” 列表框被选中时的背景 android:popupBackground=“#F3A0A0” 设置列表框的背景 android:dropDownHorizontalOffset 设置列表框的水平偏移距离 void setSelection(int n) 默认下拉列表框的第一项为选中项目,用这个方法指定第n个条目为选中条目 Object getSelectedItem() 返回列表框中选中项
1. 下拉列表步骤及事件监听
🔰下拉列表的使用一般按如下步骤进行:
- 在布局文件中定义控件
<Spinner android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/dd"/>
- 在Activity中引用
Spinner spinner = (Spinner)this.findViewById(R.id.dd);
- 创建一个适配器(Array Adapter)为Spinner提供数据,Array Adapter中的数据来源于字符串数组和XML两种方式;
方法一:使用字符串数组作为数据来源String[] provinces = new String[] { "江苏省", "浙江省", "上海市"}; // 设置Array Adapter ArrayAdapter <String> weekArray = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,provinces); // 三个参数分别为Context,布局文件,数组
方法二:使用XML作为数据来源,内容放到values目录下的strings.xml资源文件中
<resources> <string-array name="provinces_array"> <item>江苏省</item> <item>浙江省</item> <item>上海省</item> </string-array> </resources>
设置Array Adapter
ArrayAdapter provinceArray = ArrayAdapter.createFromResource(this, R.array.provinces_array, android.R.layout.simple_spinner_item); provinceArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- 将适配器与Spinner相关联
spinner.setAdapter(provinceArray);
- 创建一个监听器
class SpinnerListener implements AdapterView.OnItemSelectedListener { @Override public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, >long arg3) { String selected = arg0.getItemAtPosition(arg2).toString(); Toast.makeText(MainActivity.this, "你所在的省份是:" + selected, Toast.LENGTH_LONG).show(); }
- 绑定监听器
spinner.setOnItemSelectedListener(new SpinnerListener());
🔰如果开发者使用Spinner时己经可以确定列表选择框里的列表项,则完全不需要编写代码,也不需要设置适配器,直接使用android:entries属性来设置数组资源作为下拉列表框的列表项目,即可以省略(3)和(4)步骤。
(十一) 评分条【RatingBar】
🔰用星型来表示评分等级,设置时默认可以选择三种不同的风格;
效果如下:
属性 说明 android:isIndicator=“false” 设置是否可以与用户交互(默认true表示不可交互)
设置用户是否可以点击android:rating=“1.5” 默认的评分数,一开始就有的评分数 android:stepSize=“1.0” 点击一次增长的长度 android:numStars=“5” 表示星星数量 style=“?android:ratingBarStyleIndicator” 设置风格 ?android:ratingBarStyle
大号星星?android:ratingBarStyleIndicator
正常星星?android:ratingBarStyleSmall
小号星星float getRating() 获取当前评分,与参数rating一致
1. 星级进度改变时监听事件
// 用RatingBar.OnRatingBarChangeListener()实现星级进度改变时触发事件
RatingBar pingfen = this.findViewById(R.id.pingfen);
TextView buchang = this.findViewById(R.id.bu);
pingfen.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
buchang.setText("你的评分结果为:"+pingfen.getRating()+"");
}});
(十二) 帧布局【FrameLayout】
🔰它是一种在Android终端屏幕上开辟一块空白区域的布局方式,放置在空白区域的组件必须对其到屏幕的左上角;
🔰帧布局的大小由子元素中尺寸最大的那个来决定;
1.【TabHost】组件
🔰继承自FrameLayout,是带Tab选项卡的容器,包含
TabWidget
和FrameLayout
两部分;
🔰TabWidget
是每个Tab选项卡标签按钮;FrameLayout
是每个Tab选项卡的内容;
2. 定义布局文件
🔰Tab选项卡的用户界面布局文件的设计需要遵循下表的要求定义;
属性 说明 TabHost 可自定义id TabWidget 必须设置 android:id
为@android:id/tabs
FrameLayout 必须设置为 android:id
为@android:id/tabcontent
3. 选项卡标签位置放置
1)放置用户界面顶部
布局内容需要用【LinearLayout来布局】;
2)放置用户界面底部
布局内容需要用【RelativeLayout】来布局,并且在【TabWidget】内添加android:layout_alignParentBottom="true"
4. TabSpec常用方法
方法名 | 说明 |
---|---|
setIndicator(CharSequence label | 用于指定选项卡显示的标签名 |
setIndicator(CharSequence label, Drawable icon) | 用于指定选项卡显示的标签名及图片 |
setIndicator(View view) | 用于指定选项卡显示的标签View |
setContent(int viewId) | 用于指定选项卡显示的内容,通常在布局文件中定义 |
setContent(Intent intent) | 用于指定选项卡显示的内容,通常Inter用来封装启动另一个Activity |
setCurrentTab(int index) | 用于指定默认选项卡 |
5. 考试系统案例
🔰布局代码
<TabHost
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/kaoshiXT">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/danxuan"
android:orientation="vertical">
<!-- 单选布局-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/duoxuan1"
android:orientation="vertical">
<!-- 多选布局-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/jianda"
android:orientation="vertical">
<!-- 简答题布局-->
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
🔰Java代码
TabHost tabHost = (TabHost) findViewById(R.id.kaoshiXT); // 获取TabHost对象
tabHost.setup(); // 通过setup()方法加载启动Tab Host
tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("单选题").setContent(R.id.danxuan));
tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("多选题").setContent(R.id.duoxuan1));
tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("简答题").setContent(R.id.jianda));
tabHost.setCurrentTab(2); // 指定2号选项卡为默认选项卡(选项卡自动0,1,2排序)
(十三) 表格布局【TableLayout】
🔰该布局是由一系列行和列组成的网格,在这些网格的单元格中可以显示View组件;
🔰它是和TableRow配合使用的一种常用的布局管理方式,每个TableRow对应表格里的一行,TableRow的内容由单元格中的View组件组成;
🔰有多少个TableRow 就有多少行;
🔰全局属性(列属性,写在TableLayout里面)
属性名 说明 android:stretchColumns="* " 设置允许被拉伸的列的序号 android:shrinkColumns="* " 设置允许收缩的列的序号
可以设置多个列,但必须用逗号隔开android:collapseColumns=“1, 2” 设置要隐藏的列的序号 🔰单元格属性(单元格内书写)
属性名 说明 android:layout_column=“1” 指定该单元格在第几列显示 android:layout_span=“2” 合并2个单元格,即该组件占据2个单元格 🔰在使用