ScrollView的内容超出了父级的界限

时间:2014-11-05 15:00:46

标签: java android-layout android-scrollview

我在xml代码中有如下所示的布局。在ScrollView内,我想附加LinearLayouts,其后面会包含一个ImageView和一条小文字。这个,我必须以编程方式完成。加载图像时,LinearLayout将显示不确定的ProgressBar,在图像和文本添加之前将其删除。所有这些都在AsyncTasks完成。

fragment_images.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:gravity         = "center"
    android:layout_height   = "match_parent"
    android:layout_width    = "match_parent" >


    <ScrollView
        android:id              = "@+id/scrollview_fragment_species_images_vertical"
        android:layout_height   = "wrap_content"
        android:layout_width    = "wrap_content"
        android:orientation     = "vertical" 
        android:visibility      = "gone" >

        <LinearLayout
            android:layout_gravity  = "center"
            android:id              = "@+id/linlay_fragment_species_images_vertical"
            android:layout_height   = "wrap_content"
            android:layout_width    = "wrap_content"
            android:orientation     = "vertical" 
            android:visibility      = "gone" >
        </LinearLayout>
    </ScrollView>

</LinearLayout>

这是代码,它加载图像,添加和填充LinearLayouts

ScrollView scrollViewVert  = (ScrollView) getActivity().findViewById(R.id.scrollview_fragment_species_images_vertical);
scrollViewVert.setVisibility(View.VISIBLE);
LinearLayout linlayContainer = (LinearLayout) getActivity().findViewById(R.id.linlay_fragment_species_images_vertical);
linlayContainer.setVisibility(View.VISIBLE);

private void makeImageList(String jsonImages) {

    // ...

    for (int i=0; i<count_images; i++) {
        final ProgressBar progress = new ProgressBar(getActivity().getBaseContext());
        progress.setIndeterminate(true);


        final LinearLayout childContainer = new LinearLayout(getActivity().getBaseContext());
        childContainer.setGravity(Gravity.CENTER_HORIZONTAL);
        childContainer.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        childContainer.setOrientation(LinearLayout.VERTICAL);
        childContainer.setPadding(15, 15, 15, 15);


        final TextView childText = new TextView(getActivity().getBaseContext());
        childText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        childText.setPadding(15, 10, 15, 5);
        String imageSize = (Integer.parseInt(templist.get(i)[2]) / 1024) + "kB";
        childText.setText(imageSize);
        childText.setTextColor(Color.BLACK);


        final ImageView child = new ImageView(getActivity().getBaseContext());
        child.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        child.setPadding(15, 15, 15, 0);


        URL url = null;
        try {url = new URL(address);} // just an internet address. this works!
        catch (MalformedURLException e) {e.printStackTrace();}

        linlayContainer.addView(childContainer);
        childContainer.addView(progress);


        new AsyncTask<URL, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(URL... params) {
                Bitmap image = null;
                try {image = BitmapFactory.decodeStream(params[0].openConnection().getInputStream());}
                catch (IOException e) {e.printStackTrace();}

                return image;
            }

            protected void onPostExecute(Bitmap image) {
                child.setImageBitmap(image);
                childContainer.removeView(progress);
                childContainer.addView(child);
                childContainer.addView(childText);
            }
        }.execute(url);
    }
}

不知怎的,我的布局是垂直移动的,我不明白为什么。下面的图像显示了问题。向左,达到ScrollView的上限,一张图像不可见;右边是ScrollView的下端,有太多的可用空间。也许它不相关,但这是一个标签片段活动。我不知道如何进一步缩小错误范围。

提前致谢。

Upper limit of ScrollView Lower limit of ScrollView

1 个答案:

答案 0 :(得分:0)

我发现错误,虽然我不明白,这里到底出了什么问题:

<LinearLayout
        android:layout_gravity = "center"
...

LinearLayout内的ScrollView必须仅水平居中,而不是水平垂直居中。因此,上述行必须如此纠正:

        android:layout_gravity = "center_horizontal"

如果有人会这么善良并解释,为什么layout_gravity - 参数不仅限于父视图,而是显然是整个屏幕或任何根视图......我不明白