亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android開發(fā)之StackView用法和遇到的坑分析

 更新時間:2019年03月26日 12:10:23   作者:水中魚之1999  
這篇文章主要介紹了Android開發(fā)之StackView用法和遇到的坑,結(jié)合實例形式分析了Android StackView圖片操作用法及常見問題解決方法,需要的朋友可以參考下

本文實例分析了Android開發(fā)之StackView用法和遇到的坑。分享給大家供大家參考,具體如下:

關(guān)于StackView網(wǎng)上已經(jīng)有很多內(nèi)容了

這里我著重將一些使用過程中遇到的坑吧

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

很多人加完圖片后發(fā)現(xiàn)圖片不顯示,這里可能有兩個原因:

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

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>

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

最新評論