最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

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

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

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

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

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

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

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

2.將圖片轉為Bitmap,然后再將其質量和大小進行壓縮。

二、加完圖片后發(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>

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

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

相關文章

最新評論

隆昌县| 额济纳旗| 上高县| 曲麻莱县| 郎溪县| 张掖市| 自贡市| 莱阳市| 天峨县| 长宁县| 三都| 琼结县| 台东市| 永康市| 麻城市| 临高县| 盐池县| 绥芬河市| 琼海市| 广州市| 许昌县| 万安县| 宝应县| 汽车| 丹寨县| 肇东市| 延庆县| 通山县| 黔江区| 阿拉善左旗| 渭南市| 微山县| 台南市| 临湘市| 尉犁县| 田东县| 深州市| 龙游县| 成安县| 长汀县| 甘南县|