我在片段中显示ImageView以在图像之间滑动。感谢this帖子,我可以将图像和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>
答案 0 :(得分: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;
}
答案 1 :(得分: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;
}