我是靠谱客的博主 失眠小霸王,这篇文章主要介绍Android开发之StackView用法和遇到的坑分析,现在分享给大家,希望可以做个参考。

本文实例分析了Android开发之StackView用法和遇到的坑。分享给大家供大家参考,具体如下:

关于StackView网上已经有很多内容了

这里我着重将一些使用过程中遇到的坑吧

先看下效果,和很多人一样

很多人加完图片后发现图片不显示,这里可能有两个原因:

一、直接闪退,然后报错。一般会有头这么一句话:

Failed to allocate a 74649612 byte allocation with 16765728 free bytes and 59MB until OOM

提示一个很明白了,内存溢出,具体原因是关于Android的内存分配机制的这里就不详细讲了

这不经事StackView常见的问题,所有添加图片的活动都可能发生

怎么办呢?主要有两种办法:

1.暴力直接,用图片转换器(或者直接用windows自带画图工具)将图进行压缩。但很明显治标不治本。

2.将图片转为Bitmap,然后再将其质量和大小进行压缩。

二、加完图片后发现图片不显示

这个一般来说是代码本身的问题

检查下你List对象和Adapter对象的一些名字是否一致

这里以MainActivity为例(改编自疯狂Android)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class MainActivity extends Activity { StackView stackView ; int[] imageIds = new int[]{ R.drawable.a0,R.drawable.a00,R.drawable.a1,R.drawable.a02, R.drawable.a1,R.drawable.a2, R.drawable.a3 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); stackView = (StackView) findViewById(R.id.mStackView); //创建一个list 对象 元素是MAP List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>(); for( int i = 0 ; i < imageIds.length ; i++ ){ Map<String,Object> listItem = new HashMap<String, Object>(); listItem.put("image",imageIds[i]); listItems.add(listItem); } //创建一个Simple Adapter SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.photo, new String[]{"image"}, new int[]{R.id.image1}); stackView.setAdapter(simpleAdapter); } public void prev(View source){ //显示上一个组件 stackView.showPrevious(); } public void next(View source){ //显示下一个组件 stackView.showNext(); } }

注意检查一下listItems和simpleAdapter用来存放图片的变量名是否一致

比如我这儿是叫做image

这是比较常见的一种错误,如果是其他错误则需要大家再去Google一下了

鉴于很多同学表示不知道cell (我这里叫做photo)这个layout是什么

其实就是一个很简单的layout 向自定义listView等等,很多时候都得用上这种自定义的layout

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/image1" android:layout_width="200dp" android:layout_height="200dp" android:scaleType="fitXY" android:layout_gravity="center"/> </LinearLayout>

我遇到的坑大概就这些了,最后附上布局文件:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <StackView android:id="@+id/mStackView" android:layout_width="match_parent" android:layout_height="wrap_content" android:loopViews="true"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:orientation="horizontal"> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="prev" android:text="上一个" /> <Button android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="next" android:text="下一个" /> </LinearLayout> </RelativeLayout>

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android数据库操作技巧总结》及《Android资源操作技巧汇总》

希望本文所述对大家Android程序设计有所帮助。

最后

以上就是失眠小霸王最近收集整理的关于Android开发之StackView用法和遇到的坑分析的全部内容,更多相关Android开发之StackView用法和遇到内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(125)

评论列表共有 0 条评论

立即
投稿
返回
顶部