Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将图像加载到滚动视图会导致图像超出滚动视图_Java_Android_Xml - Fatal编程技术网

Java 将图像加载到滚动视图会导致图像超出滚动视图

Java 将图像加载到滚动视图会导致图像超出滚动视图,java,android,xml,Java,Android,Xml,我正在将图像从Android Camera文件夹动态加载到水平滚动视图中。问题是滚动视图的末尾(右侧)有一个很大的空间,当我填充滚动视图(左侧)时,图像被推离屏幕前部。加载的图像越多,滚动视图末端的间隙就越大,从前面推出的图像就越多 xml只是作为父级的滚动视图,作为子级的线性布局 <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://sc

我正在将图像从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>

代码使用asynctask在中加载图像

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]);
  }
私有类loadImages扩展异步任务{
@凌驾
受保护的布尔doInBackground(上下文…参数){
文件sdCard=Environment.getExternalStorageDirectory();
dir=新文件(sdCard.getAbsolutePath()+“/DCIM/Camera”);
dir.mkdirs();
ImageView gtbImage=null;
if(dir.isDirectory()){
String[]children=dir.list();
for(int i=0;i
这是一张更好地描述scrollview结尾的图片。请注意结尾处的大空白点。图片越多,空间越大


对此我感到非常困惑,我确信我错过了一些简单的东西,但经过一次又一次的研究,我遇到了一个死胡同,需要一双更聪明的眼睛来看看:)

尝试删除
android:layout\u gravity=“center\u horizontal”
HorizontalScrollView
xml中的
行。我之前也遇到过类似的问题


相反,您应该能够按照建议在
水平滚动视图上使用
android:fillViewPort=“true”
,或者同时使用
android:layout\u gravity=“center”
android:gravity=“center”
在您的
线性布局中
如建议。

如果您不添加填充和边距怎么办?如果您可以访问Jellybean设备(或模拟器),您可以启用“显示布局边界”选项以查看发生了什么。@Niek注释了p.setMargins(5,5,5,5);和gtbImage.setPadding(1,1,1);是否有完全相同的结果我删除了layout_gravity,并添加了fillViewport,同样的问题仍然存在更新:我删除了android:layout_gravity=“center”我不完全理解为什么它会修复它,但你的建议是修复它的关键!谢谢@Niek!太好了!它确实与
滚动视图中的某个东西居中有关,这会产生这些奇怪的问题。