服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android开发之StackView用法和遇到的坑分析

Android开发之StackView用法和遇到的坑分析

2022-10-09 15:17水中鱼之1999 Android

这篇文章主要介绍了Android开发之StackView用法和遇到的坑,结合实例形式分析了Android StackView图片操作用法及常见问题解决方法,需要的朋友可以参考下

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

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

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

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

Android开发之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程序设计有所帮助。

原文链接:https://blog.csdn.net/qq_43377749/article/details/84571335

延伸 · 阅读

精彩推荐