2013-07-04 5 views
-1

Я показываю ImageView в своих фрагментах, чтобы провести между изображениями. Благодаря сообщению this я мог бы приспособить изображение и изображение к ширине и высоте исходного изображения. Но если изображение всегда будет в левом верхнем углу. Затем я изменил макет фрагмента, попытался центрировать изображениеView горизонтально и вертикально. Но после того, как окружающие его с RelativeLayout или LinearLayout, я получил IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.change layout cause illegalStateException

Не работает код в комментариях:

Фрагмент:

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

После изменения файла XML, вы должны вернуть корень завышены view.Which ваш ViewGroup.

@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; 
    } 
+0

извините за эту глупость, глупую ошибку * стыд * – lis

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; 
}