2011-08-25 64 views
0

下面的代碼示例:Android的 - 問題的RelativeLayout兒童GONE知名度

public class CustomImageVIew extends LinearLayout { 
     private Image mImage = null; 
     private ImageView mImageView; 
     private ProgressBar mProgressBar; 
     private RelativeLayout mLayout; 
     public static enum State {NOT_LOADED, IMAGE_LOADING, IMAGE_LOADED}; 
     private State state; 



     public CustomImageVIew(Context context) { 
      this(context, null); 
     } 

     public CustomImageVIew(Context context, AttributeSet attrs) { 
      super(context, attrs); 

      View v = View.inflate(getContext(), R.layout.gallery_image_view, null); 
      mLayout = (RelativeLayout) v; 
      addView(mLayout); 

      mImageView = (ImageView) mLayout.findViewById(R.id.gallery_image_view_image); 
      mProgressBar = (ProgressBar) mLayout.findViewById(R.id.gallery_image_view_progress_bar); 
      setState(State.NOT_LOADED); 
     } 

     private void setState(State newState) { 
      if (state != newState) { 
       state = newState; 

       switch (state) { 
       case NOT_LOADED: 
        mImageView.setVisibility(View.GONE); 
        mProgressBar.setVisibility(View.GONE); 
        break; 
       case IMAGE_LOADING: 
        mImageView.setVisibility(View.VISIBLE); 
        mProgressBar.setVisibility(View.VISIBLE); 
        break; 
       case IMAGE_LOADED: 
        mImageView.setVisibility(View.VISIBLE); 
        mProgressBar.setVisibility(View.GONE); 
        break; 
       } 
      } 
     } 


     public State getState() { 
      return state; 
     } 


    } 





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

     <ImageView 
      android:id="@+id/gallery_image_view_image" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
     <ProgressBar 
      style="?android:attr/progressBarStyleLarge" 
      android:layout_width="wrap_content" 
      android:id="@+id/gallery_image_view_progress_bar" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_centerHorizontal="true" 
      android:clickable="false"/> 



    </RelativeLayout> 

當我打電話的setState(State.IMAGE_LOADED)的onCreate方法沒有任何反應之外。 它可能是什麼問題?

+0

我不知道你是怎麼做的,因爲setState是一個私有方法 – damson

回答

0

我想你真的不需要自定義視圖(java文件)。您可以使用在xml中聲明的LinearLayout和您的活動的onCreate()方法執行此操作。 XML讓你的生活變得更輕鬆。編碼時應遵循Android的最佳實踐。