Organizing common controls in android

1. Primary controls

1. Screen display
px: is the smallest unit that can be displayed on the screen of a mobile phone. The clearer the screen of the same size, the higher the density of pixels, and the greater the resolution measured in px.
dp : It is only related to the size of the screen. For screens of the same size, the measurement resolution of dp is the same.
sp: sp is similar to dp, sp is used to set the size of the text, and sp will become larger when a large font is set.
The connection between dp and px depends on the pixel density on the specific device. The density is the density parameter in DisplayMetrics.
When density=1.0, it means that one dp corresponds to one px,
When density=1.5, it means that two dp correspond to three px values.
When density=2. Indicates that one dp corresponds to two px values;
The size functions in android are all in px, LayoutParams width height setPadding, etc.,
The parameter units are all px, so the conversion between dp and px is required;

2. Simple layout
layout_gravity: Specifies the alignment of the view with the parent view,
visibility: specifies the visibility of the view;
1) visible: visible
2) invisible: invisible, but still occupying a position
3) Gone: invisible and does not occupy a position

Three methods of viewGroup:
1) addView : add a view to the layout
2) removeView: Specify the deleted view in the layout
3) removeallView: remove all views in the layout

LinearLayout: (linear layout)
orientation: specifies the orientation of the linear layout
1) horizontal: Indicates horizontal layout (default state)
2) vertical: Indicates vertical layout
Gravity: Specifies the alignment of the layout's inner views with this layout

* Focus:
The difference between Layout_Gravity and Gravity:
Gravity specifies the relative position of the element in the current layout
layout_Gravity : 所指定的是当前布局在整体布局中的相对位置

滚动视图:Scrollview
需要注意三点:
1)垂直方向滚动时: width要设置为match_parent height要设置为wrap_content
2)水平方向: width 要设置为wrap_content, height 要设置为match_parent
3) 滚动视图的节点下面必须只能挂着一个子布局节点,否则会报错;

3.简单控件
TextView
1)textAppearance 设置文本风格;
跑马灯常用属性
1)singleLine : 指定文字是否单行显示
2)ellipsize : 指定文字超出范围后的省略号方式
省略号取值说明
start: 省略号在开头
middle: 省略号在中间
end: 省略号在末尾
marquee: 跑马灯显示
focusable: 指定是否获得焦点
focualbeInTouchMode: 指定在触摸时是否获取焦点,跑马灯效果要求设置为ture

ImageView(图像显示控件)
1)scaleType:(默认是fitCenter)
fitXY: 拉伸图片使其图片正好填满视图(图片可能被拉伸变形)
fitStart: 拉伸图片使其位于视图上部
fitcenter 拉伸图片使其图片位于视图中间
fitend : 拉伸图片使其位于视图下部
center :保持视图原尺寸,在视图中间
centerCrop : 拉伸图片使其充满视图,位于视图中间
centerInside: 图片位于视图中间

android 自带截图功能:
setDrawingCacheEnabled: 设置绘图缓存的可用状态
isDrawingCacheEnabled:判断该控件的绘图缓存是否可用;
setDrawingcahcheQuality: 设置绘制缓存的质量
getDrawingCache: 获取该控件的绘制缓存结果 bitmap类型
setDrawingCacheBackgroundColor: 设置绘图的背景颜色:

imageButton 与button的区别:
1)Button既可以显示文本也可以显示图形; imageButton只能显示图形不能显示文本;
2)imageButton 可按比例拉伸,button上的图会拉伸变形
3)Button只能显示背景一张图片,imageButton分别在前景和背景显示两张图片,实现图片叠加效果;

4.图形 Drawable
常用的xml图形文件,描述形状定义的图形,---shape图形
形状图形的定义文件以shape元素为根节点,根节点下定义了6个节点:
1)corners:圆角
是shape的下级节点:
bottomLeftRadius 左下圆角的半径
bootomRightRadius 右下圆角的半径
topLeftRadius 左上圆角的半径
topRightRadius 右上圆角的半径
2)gradien:渐变
用来描述形状内部颜色渐变
type: 表示渐变类型
1)Linear : 线性渐变
2)radial: 放射性渐变,其实颜色就是圆心颜色
3)sweep: 滚动渐变
3)padding:间隔
4)size: 尺寸
5)soild:填充
描述形状内部的填充色彩
6)stroke:描边
用来描述
形状图形四周边线的规格定义

二,中级控件
1.相对布局 relativeLayout
下级视图的位置是相对位置,有具体的参照物才能确定位置;

2.特殊按钮
在android中,CompoundButton类是抽象的复合按钮,因为是抽象的,所以不能使用,
在实际开发中用compoundButton类的几个派生类,主要有checkBox复选框,
单选按钮 RadioButton,switch开关按钮
1) 在布局文件中主要使用下面两个属性
checked: 指定按钮的勾选状态
button: 指定左侧勾选图标的图形
2)在代码中使用4种方法进行设置
setChecked:设置按钮的勾选状态
setButtonDrawable: 设置左侧图标勾选的图形
setOncheckedChangeListener: 设置勾选状态变化的监听
isChecked: 判断按钮是否被勾选

2.开关按钮switch
switch是开关按钮,在android4.1.2开始支持该控件,
1)textOn: 设置右侧开启时的文本
2)textOff: 设置左侧关闭时的文本
3)switchPadding 设置左右两个开关按钮之间的距离
4)thumbTextPadding 设置文本左右两边的距离,如果设置该属性switchPadding就会失效
5)thumb 设置开关轨道的背景
6)track: 设置开关标识的图标

3.编辑框;
android两种编辑框,分别是文本编辑框EditText,与自动完成编辑框AutoCompleteTextView,
常见的四种编辑处理:
更换光标,更换边框没自动隐藏输入法,输入回车符自动换行;
editText的属性
1)maxLength:允许输入的最大长度,
2)hint 指定提示文本的内容
3)textColorHint: 指定提示文本的颜色
inputType:指定输入的文本类型;

1.更换编辑框光标
需要处理两个主要的属性:
1)cursorVisiable: 指定光标是否可见,
2)textCursorDrawable: 指定光标的图片

2,更换编辑框的边框

3.关闭软键盘
输入法通过系统INPUT_METHOD_SERVOCE管理,所以隐藏输入法也需要通过该服务来实现
两种方法实现隐藏软件盘
1)调用toggleSoftInput
2)hideSoftInptFromWindow

4.自动完成编辑框 AutocompleteTextView
自动完成编辑框,一般用于搜索编辑框,在电商App的搜索框输入商品文字时,下方会自动弹出提示词列表,方便用户快速选择商品

属性和设置方法:
1)completionHint :设置下拉列表底部提示文字
2)completionThreshold : 设置至少输入多少个字符会显示提示
3)dropDownHorizontalOffset :设置下拉列表与文本之间的水平偏移
4)dropDownVerticalOffset: 设置下拉列表与文本框之间的垂直偏移
5)dropDownHeight :设置下拉列表的高度
6)dropDownwidth: 设置下拉列表的宽度


5.使用intent传递消息
intent的属性:
1)component 用于指定intent来源于目的
2)Action 用于指定intent的操作行为
3)Data 用于指定要操作的数据路径
4)category 用于指定intent的操作类别
5)type 用于指定data的类型定义
6)extras 用来指定装载的参数信息
7)flags : 用于指定intent的运行模式

向下一个activity传参
/* 第一个页面 向下一个acitivity传递参数*/
Intent intent=new Intent(SixActivity.this,SevenActivity.class);
Bundle b=new Bundle();
b.putString("name",editText.getText().toString());
intent.putExtras(b);
startActivityForResult(intent,0);
/* 第二个页面 接受数据
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String name = extras.getString("name", "");
textView.setText(name);

向上一个activity返回参数:
//这是第二个页面 向上一个acitivty返回参数
Intent intents = new Intent();
Bundle bundle =new Bundle();
bundle.putString("job",editText.getText().toString());
intents.putExtras(bundle);
setResult(Activity.RESULT_OK,intents);
finish();

//这是第一个页面 接受返回的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data!=null){
String job = data.getStringExtra("job");
// String job = extras.getString();
textView.setText(job);
}
}

三。高级控件

1.时间控件
日期选择器 DatePicker
datePicker并非是弹窗模式,而是直接在页面上占据一块区域,并不会自动关闭
因此,datePicker不适合直接使用,在实际开发中用datePickerDialog;

2.基本适配器
1)ArrayAdapter 适用于纯文本的列表数据,
2)SimpleAdapter 适用于带图标的列表数据
在实际开发中常常有更复杂的列表,同一项中存在多个控件,
android 提供了更强大的基本适配器BaseAdapter
在BaseAdapter中主要实现三个方法
构造函数: 指定适配器需要处理的数据集合
getCount : 获取数据项的个数
getView: 获取每项的展开视图,并对每项内部的控件做业务的处理

3.Fragment 碎片
1)当我们页面继承activity 不能使用android v4 Fragment
否则app会报错
2)Fragment的生命周期
1)onAttach 与activity结合,可以再该方法中实例化一个activity对象
2)onCreate 创建碎片
3)onCreateView 创建碎片视图
4)onActivityCreated 在活动页面创建完毕之后调用
5)onStart 开启碎片
6)onResume 恢复
7)onPause 暂停
8) onStop 停止
9)onDestoryView 回收碎片视图
10)onDestory 销毁
11)onDetach 与activity分离

Fragment 静态注册
每个Fragment都有对应的布局文件,使用的方式可分为静态注册和动态注册
静态注册是咋布局文件中直接放置fragment节点,类似于普通控件,可被多个文件同事引用,
静态注册一般用于某个通过的页面部件,每个活动页面均可直接引用该部件;

动态注册
在实际开发中动态注册用的更多,动态注册在代码中动态添加fragment,

关于Fragment的总结
1)动态注册时, Fragment的onCreate操作在activity之后,
2)onActivityCreated,该方法在acrtivty的onCreate操作之后,可见该方法在页面的创建之后才调用,
进入第一个Fragment,实际只加载第一个页面和第二个页面,并没有加载全部的gragment,无论当前位于哪个页面,系统都会只加载当前页面和相邻的前后两页,一旦发生页面切换,相邻的页面就被加载,非相邻的就会被回收,

4.广播
广播用于android组件之间的灵活通讯,与activity的区别在于:
1)activity只能一对一通讯,broadcast可以一对多,一人发送广播,多人接受处理,
2)对于发送者来说,不需要考虑接受者有没有在工作,接受者在工作就接受,不在工作就丢弃广播
3)对于接收者来说,会受到各式各样的广播,所以接收者要自行过滤符合条件的广播;

广播的重要方法:
sendBroadcast: 发送广播;
registerReceiver: 注册接收器:一般在onstart和onResume中注册
unRegisterReceiver:注销接收器,一般在onstop或onPause中注销;

5.定时器 AlarmManager
AlarmManager是android提供的一个全局定时器,
定时器能够完美的实现广播功能,就是alarmManager与PengdingIntent相互配合使用;
pendingIntent 的意思是延迟的意图,只要不是立即传递的消息,都要用pendingIntent;
pengingIntent调用getBroadCast方法,表示这次携带的消息用于发送广播

AlarmMananger的参数
1)表示定时器的类型,一般是AlarmMananger.RTC_WAKEUP,
2)表示任务的执行时间
3)表示携带消息的延迟任务

6.振动器 vibrator
振动器的对象从服务器VIBRATOR_SERVICE中获取,
振动器的主要方法“:
hasVibrator : 判断设备是否拥有振动
vibrate 振动手机,可设定单次 的振动时长,是否重复振动
cancel 取消振动
使用振动的时候需要添加权限、




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324647931&siteId=291194637