快速連結

2013年2月23日

讓ViewGroup建立實體物件

當想要建立ViewGroup物件時,卻發現不能用ViewGroup直接建立物件,因為他是abstract(抽象)類型,必須要再建立一個實體類別去繼承ViewGroup。
實體類別的程式碼下收:

package com.bear.myclass;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;


public class MyViewGroup extends ViewGroup {

 public MyViewGroup(Context context) {
  super(context);
  // TODO 自動產生的建構子 Stub
 }

 @Override
 protected void onLayout(boolean arg0, int l, int t, int r, int b) {
  // TODO 自動產生的方法 Stub
  // 要讓此ViewGroup內的Child顯示出來,必須要寫這段
  int childCount = getChildCount();
  Log.d("MyViewGroup","Layout四邊長 -- left:"+l+" right:"+r+" top:"+t+" bottom:"+b);
  
  
  for (int i = 0;i < childCount;i++){
   final View child = getChildAt(i);
   
   child.setVisibility(View.VISIBLE);
   child.measure(0,0);//r-l, b-t);
   child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
  }

 }

}


沒有留言:

張貼留言

歡迎大家留言提問,我會答的都會盡力回答!
如果太久沒出現回應就是我又忘記回來看留言了TAT