Android開發(fā)之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程序設計有所幫助。
相關文章
Android開發(fā)設計nowinandroid構建腳本學習
這篇文章主要為大家介紹了Android開發(fā)設計nowinandroid構建腳本學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android實現(xiàn)基于ViewPager的無限循環(huán)自動播放帶指示器的輪播圖CarouselFigureView控件
這篇文章主要介紹了Android實現(xiàn)基于ViewPager的無限循環(huán)自動播放帶指示器的輪播圖CarouselFigureView控件,需要的朋友可以參考下2017-02-02
Android kotlin使用注解實現(xiàn)防按鈕連點功能的示例
這篇文章主要介紹了Android kotlin使用注解實現(xiàn)防按鈕連點功能的示例,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下2021-03-03
Android TextView實現(xiàn)垂直滾動效果的方法
這篇文章主要介紹了Android TextView實現(xiàn)垂直滾動效果的方法,結合實例形式簡單分析了Android TextView控件垂直滾動效果的相關屬性功能與設置技巧,需要的朋友可以參考下2016-10-10
Android ListView里控件添加監(jiān)聽方法的實例詳解
這篇文章主要介紹了Android ListView里控件添加監(jiān)聽方法的實例詳解的相關資料,這里提供實例幫助大家學習理解這部分內容,需要的朋友可以參考下2017-08-08
詳解Androidstudio3.0 關于Gradle報錯的問題(小結)
本篇文章主要介紹了詳解Androidstudio3.0 關于Gradle報錯的問題(小結),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android RadarView雷達圖(蜘蛛網(wǎng)圖)的實現(xiàn)代碼
這篇文章主要介紹了Android RadarView雷達圖(蜘蛛網(wǎng)圖)的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

