首页 > 系统 > Android > 正文

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

2019-12-12 00:15:54
字体:
来源:转载
供稿:网友

本文实例分析了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)

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

<?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>

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

<?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程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表