我的应用需要列出一项特定 Activity 的文本和图像。列表的每一行都有 2 列 - 1 列用于图像,1 列用于字符串。
如果图像数量很少(十个或更少),它就可以工作。当它超过 10(有时 15)时,应用程序就会崩溃。如何使其适用于十多个图像?
下面是列出文本和图像的代码。前 2 行从 strings.xml 检索数据。谢谢。
pic=res.obtainTypedArray(R.array.site_pic);
arr_site = getResources().getStringArray(R.array.site_name);
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
for(int i=0; i<num_site; i++)
{
final TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.setPadding(0, 10, 0, 10);
ImageView iv = new ImageView(this);
iv.setImageDrawable(pic.getDrawable(i));
iv.setAdjustViewBounds(true);
iv.setMaxHeight(80);
iv.setPadding(5, 5, 15, 5);
tr.addView(iv);
TextView tv1 = new TextView(this);
tv1.setText(arr_site[i]);
tv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tv1.setGravity(Gravity.LEFT);
tr.addView(tv1);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
最佳答案
如果您的图像尺寸是高分辨率,这可能就是问题所在。根据我的经验,尽一切可能减小图像尺寸 - 但保持质量至关重要。
另外,如果所有其他方法都失败了,一个简单的修复方法(如果您还没有尝试过)就是添加
android:largeHeap="true"
在内<application>
AndroidManifest.xml 文件的 标记。
以下是我自己的 list 文件中的示例:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:largeHeap="true">
关于Android - 添加太多图像时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13553179/