Android開發(fā)之StackView用法和遇到的坑分析
本文實(shí)例分析了Android開發(fā)之StackView用法和遇到的坑。分享給大家供大家參考,具體如下:
關(guān)于StackView網(wǎng)上已經(jīng)有很多內(nèi)容了
這里我著重將一些使用過(guò)程中遇到的坑吧
先看下效果,和很多人一樣

很多人加完圖片后發(fā)現(xiàn)圖片不顯示,這里可能有兩個(gè)原因:
一、直接閃退,然后報(bào)錯(cuò)。一般會(huì)有頭這么一句話:
Failed to allocate a 74649612 byte allocation with 16765728 free bytes and 59MB until OOM
提示一個(gè)很明白了,內(nèi)存溢出,具體原因是關(guān)于Android的內(nèi)存分配機(jī)制的這里就不詳細(xì)講了
這不經(jīng)事StackView常見的問題,所有添加圖片的活動(dòng)都可能發(fā)生
怎么辦呢?主要有兩種辦法:
1.暴力直接,用圖片轉(zhuǎn)換器(或者直接用windows自帶畫圖工具)將圖進(jìn)行壓縮。但很明顯治標(biāo)不治本。
2.將圖片轉(zhuǎn)為Bitmap,然后再將其質(zhì)量和大小進(jìn)行壓縮。
二、加完圖片后發(fā)現(xiàn)圖片不顯示
這個(gè)一般來(lái)說(shuō)是代碼本身的問題
檢查下你List對(duì)象和Adapter對(duì)象的一些名字是否一致
這里以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)建一個(gè)list 對(duì)象 元素是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)建一個(gè)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){
//顯示上一個(gè)組件
stackView.showPrevious();
}
public void next(View source){
//顯示下一個(gè)組件
stackView.showNext();
}
}
注意檢查一下listItems和simpleAdapter用來(lái)存放圖片的變量名是否一致
比如我這兒是叫做image
這是比較常見的一種錯(cuò)誤,如果是其他錯(cuò)誤則需要大家再去Google一下了
鑒于很多同學(xué)表示不知道cell (我這里叫做photo)這個(gè)layout是什么
其實(shí)就是一個(gè)很簡(jiǎn)單的layout 向自定義listView等等,很多時(shí)候都得用上這種自定義的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="上一個(gè)" />
<Button
android:id="@+id/button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="next"
android:text="下一個(gè)" />
</LinearLayout>
</RelativeLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android開發(fā)設(shè)計(jì)nowinandroid構(gòu)建腳本學(xué)習(xí)
這篇文章主要為大家介紹了Android開發(fā)設(shè)計(jì)nowinandroid構(gòu)建腳本學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Flutter 構(gòu)建一個(gè)常用的頁(yè)面框架
大多數(shù) App 中都會(huì)有底部導(dǎo)航欄,通過(guò)底部導(dǎo)航欄切換實(shí)現(xiàn)不同頁(yè)面之間的切換。在Flutter 中提供了 BottomNavigationBar組件實(shí)現(xiàn)底部導(dǎo)航。本篇介紹通過(guò) BottomNavigationBar和 IndexedStack構(gòu)建最為常見的 App 頁(yè)面框架。2021-05-05
Android實(shí)現(xiàn)基于ViewPager的無(wú)限循環(huán)自動(dòng)播放帶指示器的輪播圖CarouselFigureView控件
這篇文章主要介紹了Android實(shí)現(xiàn)基于ViewPager的無(wú)限循環(huán)自動(dòng)播放帶指示器的輪播圖CarouselFigureView控件,需要的朋友可以參考下2017-02-02
Android kotlin使用注解實(shí)現(xiàn)防按鈕連點(diǎn)功能的示例
這篇文章主要介紹了Android kotlin使用注解實(shí)現(xiàn)防按鈕連點(diǎn)功能的示例,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
Android TextView實(shí)現(xiàn)垂直滾動(dòng)效果的方法
這篇文章主要介紹了Android TextView實(shí)現(xiàn)垂直滾動(dòng)效果的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了Android TextView控件垂直滾動(dòng)效果的相關(guān)屬性功能與設(shè)置技巧,需要的朋友可以參考下2016-10-10
Android ListView里控件添加監(jiān)聽方法的實(shí)例詳解
這篇文章主要介紹了Android ListView里控件添加監(jiān)聽方法的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08
詳解Androidstudio3.0 關(guān)于Gradle報(bào)錯(cuò)的問題(小結(jié))
本篇文章主要介紹了詳解Androidstudio3.0 關(guān)于Gradle報(bào)錯(cuò)的問題(小結(jié)),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android RadarView雷達(dá)圖(蜘蛛網(wǎng)圖)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android RadarView雷達(dá)圖(蜘蛛網(wǎng)圖)的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03

