在 DialogFragment 从网站下载图片后,ImageView 上方出现青色条

Cyan bar appears above ImageView after downloading image from website in DialogFragment

我尝试从以下地址下载图像文件:http://i0.kym-cdn.com/photos/images/newsfeed/000/002/110/longcat.jpg

我使用的是以下设置:

MainActivity.java:

public class MainActivity
    extends ActionBarActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(savedInstanceState == null)
        {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }
    }
}

PlaceholderFragment.java:

public class PlaceholderFragment
    extends Fragment
{
    private Button button;

    public PlaceholderFragment()
    {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        button = (Button)rootView.findViewById(R.id.fragment_main_button);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                new ImageDownloadAsyncTask().execute("http://i0.kym-cdn.com/photos/images/newsfeed/000/002/110/longcat.jpg");
            }
        });
        return rootView;
    }


    public class ImageDownloadAsyncTask extends AsyncTask<String, Void, byte[]>
    {
        @Override
        protected byte[] doInBackground(String... params)
        {
            if(params.length <= 0)
            {
                return null;
            }

            byte[] imageData = null;
            String url = params[0];
            HttpURLConnection httpURLConnection = null;
            try
            {
                URL address = new URL(url);
                httpURLConnection = (HttpURLConnection)address.openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setDoInput(true);
                httpURLConnection.connect();
                if(httpURLConnection.getResponseCode() == 200)
                {
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    IOUtils.copy(httpURLConnection.getInputStream(), byteArrayOutputStream);
                    imageData = byteArrayOutputStream.toByteArray();
                }
                else
                {
                    return null;
                }
            }
            catch(MalformedURLException e)
            {
                e.printStackTrace();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                if(httpURLConnection != null)
                {
                    httpURLConnection.disconnect();
                }
            }
            return imageData;
        }

        @Override
        protected void onPostExecute(byte[] bytes)
        {
            super.onPostExecute(bytes);
            if(bytes != null) {
                ImageDisplayDialogFragment imageDisplayDialogFragment = new ImageDisplayDialogFragment();
                imageDisplayDialogFragment.setTargetFragment(PlaceholderFragment.this, 0);
                Bundle bundle = new Bundle();
                bundle.putByteArray("imageData", bytes);
                imageDisplayDialogFragment.setArguments(bundle);
                imageDisplayDialogFragment.show(getActivity().getSupportFragmentManager(), ImageDisplayDialogFragment.TAG);
            } else {
                Toast.makeText(getActivity(), R.string.downloading_file_failed, Toast.LENGTH_LONG).show();
            }
        }
    }

}

ImageDisplayDialogFragment.java

public class ImageDisplayDialogFragment extends DialogFragment
{
    private ImageView imageView;

    public static final String TAG = ImageDisplayDialogFragment.class.getName();

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.dialogfragment_imagedisplay, container, false);
        imageView = (ImageView)view.findViewById(R.id.dialog_imagedisplay_imageview);
        byte[] imageData = getArguments().getByteArray("imageData");
        Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
        imageView.setMinimumWidth(bmp.getWidth());
        imageView.setMinimumHeight(bmp.getHeight());
        imageView.setImageBitmap(bmp);
        return view;
    }
}

所以从技术上讲,我只是使用 AsyncTask 使用 commons-io 将 URL 的内容下载到字节数组中,然后我将字节数组发送到对话框片段以在 ImageView 中显示它。

对话框片段的 XML 布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/dialog_imagedisplay_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/longcat"
        android:layout_centerInParent="true"/>
</RelativeLayout>

但是,它在图像上方显示了一个奇怪的青色线条伪影,并且 ImageView 也比实际图像本身大。

图片:

我哪里出错了?

根据您提供的屏幕截图,以及它是 DialogFragment 的事实,我可以放心地假设蓝色 line/space 是对话框的标题栏(与 ActionBar 类似,但略有不同).为了禁用它,有一个 built-in 方法:

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

这将禁用 DialogFragment 的标题栏。

更多信息,请参考docs