-1

我在 Fragments 中显示一个 ImageView 以在图像之间滑动。感谢这篇文章,我可以将图像和 imageView 调整为原始图像的宽度和高度。但是如果图像总是在左上角。然后我更改了片段的布局,尝试将 imageView 水平和垂直居中。但是在用RelativeLayout或LinearLayout包围它之后,我得到了一个IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

评论中的代码无效:

分段:

ImageView image;
Bitmap bitmap = null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    image = (ImageView) (ImageView) inflater.inflate(R.layout.fragment_image_page, container, false);
    // ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_image_page, container, false);
    // image = (ImageView) rootView.findViewById(R.id.imageView);
    new ImageLoad().execute("");
    return image;
}


private class ImageLoad extends AsyncTask<String, Void, Boolean>
{
    int width;
    int height;
    @Override
    protected Boolean doInBackground(String... params)
    {
        URL url;
        try
        {
            url = new URL(
                    "http://www.....jpg");
            // !!! ERROR IN FOLLOWING LINE !!!
            bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            width = bitmap.getWidth();
            height = bitmap.getHeight();
            int bounding = getResources().getDisplayMetrics().widthPixels;

            if (bounding < width)
            {
                float ratio = width / height;
                width = bounding;
                height = (int) (width / ratio);
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);
            }
        } catch (MalformedURLException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Boolean doInBackground)
    {
        FrameLayout.LayoutParams param = (FrameLayout.LayoutParams) image.getLayoutParams();
        param.width = width;
        param.height = height;
        image.setLayoutParams(param);
        image.setImageBitmap(bitmap);
    }
}

XML 工作:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="BILD" />

XML 不工作:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="BILD" />

</RelativeLayout>
4

2 回答 2

0

onCreateView(), 供应null而不是container。你也不需要投ImageView两次。

像这样:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    image = (ImageView) inflater.inflate(R.layout.fragment_image_page, null, false);
    ...
    return image;
}
于 2013-07-04T13:13:26.793 回答
0

更改 xml 文件后,您必须返回根膨胀视图。这是您的视图组。

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {

         ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_image_page, container, false);
         image = (ImageView) rootView.findViewById(R.id.imageView);
        new ImageLoad().execute("");
        return rootView;
    }
于 2013-07-04T13:13:30.957 回答