2013-05-16 108 views
4

我試圖從XML佈局創建複合控件。 這是我要做的事:從XML佈局創建複合視圖

public class CustomButton extends LinearLayout { 

    private TextView title; 
    private TextView subTitle; 

    public CustomButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomButton, 0, 0); 

     String titleStr = a.getString(R.styleable.CustomButton_title); 
     String subTitleStr = a.getString(R.styleable.CustomButton_subTitle); 
     a.recycle(); 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.custom_button, this, true); 

     title = (TextView)findViewById(R.id.title); 
     title.setText(titleStr); 

     subTitle = (TextView)findViewById(R.id.subtitle); 
     subTitle.setText(subTitleStr); 

     //invalidate(); 
     //requestLayout(); 
    } 
} 

如果我在調試模式下運行我看到titleStr,並subTitleStr有我在XML佈局中預定義的值,所以我可以肯定地說,調用構造函數。然而,屏幕顯示爲空白,它沒有顯示任何內容,我不知道爲什麼這樣。

希望你能幫助解決這個問題。

(僅供參考,我在下面這個教程:http://www.vogella.com/articles/AndroidCustomViews/article.html

+2

請問您是否可以共享xml活動和自定義視圖佈局文件。 –

+1

您是否確認背景和文字顏色不一樣?請提供xml文件。 –

+1

包括你的全班(如果有更多的)和你的xml!確保你在覆蓋的所有功能中都調用超級。 –

回答

1

使用所有這三個視圖構造,以確保屬性初始化總是發生。

消除)調用context.getTheme(

public class CustomButton extends LinearLayout { 

    private TextView title; 
    private TextView subTitle; 

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

    public CustomButton(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public CustomButton(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomButton); 

     String titleStr = a.getString(R.styleable.CustomButton_title); 
     String subTitleStr = a.getString(R.styleable.CustomButton_subTitle); 

     LayoutInflater.from(context).inflate(R.layout.custom_button, this, true); 
     title = (TextView)findViewById(R.id.title); 
     title.setText(titleStr); 

     subTitle = (TextView)findViewById(R.id.subtitle); 
     subTitle.setText(subTitleStr); 

     a.recycle(); 
    } 
} 
2

我已經建立,其具有多個按鈕和其動畫像抽屜一個化合物視圖。 我已經創建了xml文件並在課堂上充氣。你和我的代碼有什麼區別是膨脹的觀點。

這是我的代碼。

public class ViewDashboard extends LinearLayout { 
    private Button openCloseButton; 
    private Button mBtnViewMyLoction, mBtnViewPhotos, mBtnViewFreeStuff, mBtnViewHotspot, mBtnViewMyTeams, 
        mBtnViewLeaderboard, mBtnViewLiveAction, mBtnViewHome, mBtnViewAppStats; 
    private Context mContext; 
    private boolean isVisible = false; 
    private RelativeLayout relLayTwo; 
    private int h; 

    public ViewDashboard(Context context , AttributeSet attr) { 
     super(context , attr); 
     mContext = context;  

     DisplayMetrics metric = new DisplayMetrics();    
     ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metric); 
     h = metric.heightPixels; 

     View.inflate(context, R.layout.dashboard_drawer, this); 

     relLayTwo   =  (RelativeLayout) findViewById(R.id.relLayTwo); 
     openCloseButton  =  (Button) findViewById(R.id.btnDashboard); 
     mBtnViewPhotos  =  (Button) findViewById(R.id.btnDrawerPhotos); 
     mBtnViewFreeStuff =  (Button) findViewById(R.id.btnDrawerFreeStuff); 
     mBtnViewLeaderboard =  (Button) findViewById(R.id.btnDrawerLeaderboard); 
     mBtnViewLiveAction =  (Button) findViewById(R.id.btnDrawerLiveAction); 
     mBtnViewHome  =  (Button) findViewById(R.id.btnDrawerHome); 
     mBtnViewAppStats =  (Button) findViewById(R.id.btnDrawerAppStats); 
     mBtnViewHotspot  =  (Button) findViewById(R.id.btnDrawerHotspot); 
     mBtnViewMyTeams  =  (Button) findViewById(R.id.btnDrawerMyTeams);  
     View line   =  findViewById(R.id.line); 


     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) relLayTwo.getLayoutParams(); 
     Log.d("test", "height "+params.height); 
     params.height = h*40/100; 
     relLayTwo.setLayoutParams(params); 
     relLayTwo.setVisibility(View.GONE); 

     RelativeLayout.LayoutParams paramsLine = (RelativeLayout.LayoutParams) line.getLayoutParams(); 
     paramsLine.height = h*80/100; 

     openCloseButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       LogMsg.d("USerID "+new UserID(mContext).getUserID()); 
       if ( new UserID(mContext).getUserID().equals("")){ 
        ToastMsg.showToast(mContext, "Please Login First"); 
       }else{ 
        toggle(); 
       } 
      } 
     }); 

     /* mBtnViewMyLoction.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       toggle(); 
       mContext.startActivity(new Intent(mContext, ActivityMyLocation.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)); 

      } 
     });*/ 

     mBtnViewHotspot.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       toggle(); 
       //mContext.startActivity(new Intent(mContext, ActivityHotspots.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)); 
       mContext.startActivity(new Intent(mContext, ActivityHotspots.class)); 
      } 
     }); 

     mBtnViewMyTeams.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       toggle(); 
       //mContext.startActivity(new Intent(mContext, ActivityMyTeams.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)); 
       mContext.startActivity(new Intent(mContext, ActivityMyTeams.class)); 
      } 
     }); 

     mBtnViewFreeStuff.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       toggle(); 
       //ToastMsg.showToast(mContext, "Not Available"); 
       //mContext.startActivity(new Intent(mContext, ActivityFreeStuff.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)); 
       mContext.startActivity(new Intent(mContext, ActivityFreeStuff.class)); 
      } 
     }); 

     mBtnViewAppStats.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       toggle(); 
       //mContext.startActivity(new Intent(mContext, ActivityAppStats.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)); 
       mContext.startActivity(new Intent(mContext, ActivityAppStats.class)); 
      } 
     }); 

     mBtnViewPhotos.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       toggle(); 
       //mContext.startActivity(new Intent(mContext, ActivityPhotos.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)); 
       mContext.startActivity(new Intent(mContext, ActivityPhotos.class)); 
      } 
     }); 

     mBtnViewHome.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       toggle(); 
       mContext.startActivity(new Intent(mContext, ActivityHome.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
      } 
     }); 

     mBtnViewLeaderboard.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       toggle(); 
       //mContext.startActivity(new Intent(mContext, ActivityLeaderborad.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)); 
       mContext.startActivity(new Intent(mContext, ActivityLeaderborad.class)); 
      }   
     }); 

     mBtnViewLiveAction.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       toggle(); 
       //mContext.startActivity(new Intent(mContext, ActivityLiveAction.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)); 
       mContext.startActivity(new Intent(mContext, ActivityLiveAction.class)); 
      } 
     }); 



    } 

    public void toggle() { 
     TranslateAnimation anim = null; 
     isVisible = !isVisible; 

     if (isVisible) { 
      relLayTwo.setVisibility(View.VISIBLE); 
      anim = new TranslateAnimation(0.0f, 0.0f, h*40/100, 0.0f); 
      LogMsg.d(" rel Height "+relLayTwo.getHeight()); 
     } else { 
      anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, relLayTwo.getHeight()); 
      anim.setAnimationListener(collapseListener); 
     } 

     anim.setDuration(300); 
     anim.setInterpolator(new AccelerateInterpolator(1.0f)); 
     startAnimation(anim); 
    } 

    Animation.AnimationListener collapseListener = new Animation.AnimationListener() { 
     public void onAnimationEnd(Animation animation) { 
      relLayTwo.setVisibility(View.GONE); 
     } 

     public void onAnimationRepeat(Animation animation) { 
      // not needed 
     } 

     public void onAnimationStart(Animation animation) { 
      // not needed 
     } 
    }; 

}