2016-06-11 5 views
3

안녕 내가 개발 안드로이드 꽤 새로운 해요 (뷰 그룹에서 상속) 나는 내 자신의 사용자 정의 레이아웃을 만들려고 해요 :안드로이드 사용자 정의 레이아웃에 자녀를 추가 할 수 없습니다

public class EqLayout extends ViewGroup { 

public EqLayout(Context context){ 
    super(context); 
} 

public EqLayout(Context context, AttributeSet attrs){ 
    super(context, attrs); 
} 

public EqLayout(Context context, AttributeSet attrs, int defstyle){ 
    super(context, attrs, defstyle); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    int lato = getLato(); 
    int w = getMeasuredWidth()/lato; 
    int h = getMeasuredHeight()/lato; 
    int ws = MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY); 
    int hs = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); 

    for(int i = 0; i < getChildCount(); i++){ 
     View v = getChildAt(i); 
     v.measure(ws, hs); 
    } 
} 

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    int lato = getLato(); 
    int w = (r - l)/lato; 
    int h = (t - b)/lato; 

    for(int i = 0; i < getChildCount(); i++){ 
     View v = getChildAt(i); 
     int x = i%lato, y = i/lato; 
     v.layout(x*w, y*h, (x+1)*w, (y+1)*h); 
    } 

} 

private int getLato(){ 
    int r = (int) Math.ceil(Math.sqrt(getChildCount())); 
    r = (r > 0) ? r : r+1; 
    return r; 
    } 
    } 

MainActivity :

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    EqLayout eql = (EqLayout) findViewById(R.id.eqlviewlayout); 
    for(int i = 0; i < 14; i++){ 
     Button b = new Button(this); 
     b.setText("#"+i); 
     eql.addView(b); 
    } 
    } 
} 

하고는 XML의 :

XML file

모두 작동하는 것 같다 괜찮아요.하지만 아이를 추가 할 수는 없습니다. 런타임 및 Android Studio에서 버튼을 드래그 앤 드롭하여 시도했지만 성공하지 못했습니다.

왜 그런 일이 발생하는지 압니다. 시간 내 주셔서 감사합니다. 더 자세한 정보가 필요한지 물어보십시오.

+0

'adb shell dumpsys activity top'의 출력은 무엇입니까? – pskink

+0

어떻게 그 정보를 얻을 수 있습니까? – Levenlol

+0

터미널 창에서 'adb shell dumpsys activity top'명령을 실행하여 – pskink

답변

1

자녀의 부정적인 신장이 있습니다.

int h = (t - b)/lato; 

int h = (b - t)/lato; 

와 함께 당신은 좋은 위치 :

은 그냥 onLayout이 줄을 교체!

Screenshot