温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

StackView怎么在Android中使用

发布时间:2021-04-02 16:06:24 来源:亿速云 阅读:129 作者:Leah 栏目:移动开发

今天就跟大家聊聊有关StackView怎么在Android中使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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

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>

看完上述内容,你们对StackView怎么在Android中使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI