溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

StackView怎么在Android中使用

發(fā)布時間:2021-04-02 16:06:24 來源:億速云 閱讀:145 作者:Leah 欄目:移動開發(fā)

今天就跟大家聊聊有關(guān)StackView怎么在Android中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

一、直接閃退,然后報錯。一般會有頭這么一句話:

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

提示一個很明白了,內(nèi)存溢出,具體原因是關(guān)于Android的內(nèi)存分配機制的這里就不詳細講了

這不經(jīng)事StackView常見的問題,所有添加圖片的活動都可能發(fā)生

怎么辦呢?主要有兩種辦法:

1.暴力直接,用圖片轉(zhuǎn)換器(或者直接用windows自帶畫圖工具)將圖進行壓縮。但很明顯治標不治本。

2.將圖片轉(zhuǎn)為Bitmap,然后再將其質(zhì)量和大小進行壓縮。

二、加完圖片后發(fā)現(xiàn)圖片不顯示

這個一般來說是代碼本身的問題

檢查下你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);
    //創(chuàng)建一個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);
    }
    //創(chuàng)建一個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>

看完上述內(nèi)容,你們對StackView怎么在Android中使用有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI