布局属性和优化
常用UI的布局
1.LinearLayout: 线性布局
用来控制其子View以水平和垂直方式展开
2.RelativeLayout: 相对布局
用来控制其子view以相对定位的方式进行布局显示
3.FrameLayout: 帧布局
每一个子view都代表一个画布,后面出现的会覆盖前面的画面
通过view的android:layout_gravity属性来指定子视图的位置
-
常用视图的属性:
1.视图的常用属性
image.png
内边距的属性:

外边距的属性

只针对RelativeLayout:
相对兄弟视图定位
同方向对齐属性:

反对向对齐属性:

相对于父视图:

对于权重:

ListView的理解使用


经常使用的适配器:
ArrayAdaptater





BaseAdapter: 需要重写四个方法:

四个方法:
1.getCount() : 得到集合的个数
2.getItem() : 根据position得到对应的数据对象
3.getItemId(): 返回对应数据的ItemID
4.getView(): 得到Item视图对象
其中 convertView可以复用
为null 没有复用,我们必须加载布局,并赋值,不为null,直接用此视图对象
后面:找到子view找到对应的数据,设置数据 给listView的Item点击监听
listView.setOnItemClickListenner(listener)
item的长按监听
listView.setOnLongItemClickListener(listener)
--------------优化--------------
第0层:
不优化
每次都会执行getView(),都会执行 convertView=View.inflate(R.layout.XXX);
问题:效率太低,再快速滑动时会有卡顿,在数据多的时候甚至内存溢出
第1层:
复用convertView
if(convertView==null){
convertView=View.inflate(R.layout.XXX); //n+1
}
问题: 每次执行getview()都需要执行convertView.findViewByid()得到子view
第二层:
使用ViewHolder,减少findViewByid()的次数 减到n+1次
ViewHolder holder=null;
if(convertView==null){
convertView=View.inflate(R.layout.XXX); //n+1
holder=new ViewHolder();
holder.imageView=(ImageView)convertView.findViewById(xxx);
holder.textView=(TextView)convertView.findViewById(yyy);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
person p= data.get(position);//之前创建的类
holder.imageView.setImageResource(p.getIco());
holder.textView.setText(p.getName());
static class ViewHolder(){
ImageView imageView;
TextView textView;
}
问题1:
对于联网获取列表数据,如果数据量太大(比如超过100000)条甚至更多一次取出来显示,太慢太消耗流量
问题2.对于联网获取列表数据,如果包含图片数据,每次请求获取显示,太慢太耗流量
第三层:
图片三级缓存处理(LRu算法)
1.内存缓存,优先加载,速度最快
2.本地缓存,次优先加载。速度稍快
3.网络缓存,最后加载,速度由网络速度决定(浪费流量)
第4层:
对数据列表进行分页加载显示
1.自己做:通过Scroll监听
listView。setOnScrollListener(scrollListenter),当到达底部时加载下一页列表数据并显示
2.使用第三方开源框架:Aandroid-PullToRefresh或其他
Style和Theme
1.style: 多个视图标签属性的集合
好处:复用标签属性
目标: 布局文件中的视图标签
Theme: 本质也是style
好处:复用标签属性
目标:功能清单文件中整个应用Activity