【发布时间】:2013-02-21 21:52:23
【问题描述】:
我正在将 Android Camera 文件夹中的图像动态加载到水平滚动视图中。问题是滚动视图的末端(右侧)有一个巨大的空间,并且当我填充滚动视图(向左)时,图像被推离前面的屏幕。加载的图像越多,滚动视图末端的间隙就越大,被推到前面的图像就越多。
xml 只是作为父级的滚动视图,作为子级的线性布局。
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal" >
<LinearLayout
android:id="@+id/imageList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
代码使用异步任务加载图像。
private class loadImages extends AsyncTask<Context, ImageView, Boolean> {
@Override
protected Boolean doInBackground(Context... params) {
File sdCard = Environment.getExternalStorageDirectory();
dir = new File(sdCard.getAbsolutePath() + "/DCIM/Camera");
dir.mkdirs();
ImageView gtbImage = null;
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
File imageLocation = new File(dir.getAbsolutePath() + "/" + children[i]);
Bitmap myBitmap = decodeFile(imageLocation, 250, 250);
gtbImage = new ImageView(getApplicationContext());
gtbImage.setImageBitmap(myBitmap);
imageList = (LinearLayout) findViewById(R.id.imageList);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
p.setMargins(5, 5, 5, 5);
gtbImage.setLayoutParams(p);
gtbImage.setPadding(1, 1, 1, 1);
gtbImage.setBackgroundColor(Color.WHITE);
gtbImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
publishProgress(gtbImage);
}
}
return null;
}
protected void onProgressUpdate(ImageView... v) {
imageList.addView(v[0]);
}
这是一个更好地描述滚动视图末端的图像。注意最后的大空白点。图片越多,空间就越大。
对此感到非常困惑,我确信它是我想念的一些简单的东西,但经过一遍又一遍的研究,我已经走到了死胡同,需要一双更聪明的眼睛来看看:)
【问题讨论】:
-
如果不添加内边距和边距怎么办?如果您可以访问 Jellybean 设备(或模拟器),您可以启用“显示布局边界”选项以查看发生了什么。
-
@Niek 注释掉 p.setMargins(5, 5, 5, 5);和 gtbImage.setPadding(1, 1, 1, 1);有完全相同的结果