2015-02-11 101 views
0

我寫了下面的ViewGroup擴展視圖組強制子維度

public class myViewGroup extends ViewGroup{ 

List<View> qResult; 
List<Point> qLoc; 
ImageView qImage; 


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

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

public QueryViewLayout(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    qResult = new LinkedList<View>(); 
    qLoc = new LinkedList<Point>(); 
    qImage = null; 
} 

public void addMainView(ImageBorderView view){ 
    qImage = view; 
    super.removeAllViews(); 
    super.addView(view); 
} 

public void addResultView(View result, Point loc){ 
    super.addView(result); 
    qResult.add(result); 
    qLoc.add(loc); 
} 

/** 
* Any layout manager that doesn't scroll will want this. 
*/ 

@Override 
public boolean shouldDelayChildPressedState() { 
    return false; 
} 


@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int count = getChildCount(); 

    // Measurement will ultimately be computing these values. 
    int maxHeight = 0; 
    int maxWidth = 0; 
    int childState = 0; 



    // Only main view affects the layouts measure 

    if (qImage != null) { 
     if (qImage.getVisibility() != GONE) { 
      // Measure the child. 
      qImage.measure(widthMeasureSpec, heightMeasureSpec); 
      maxWidth = qImage.getMeasuredWidth(); 
      maxHeight = qImage.getMeasuredHeight(); 
      childState = qImage.getMeasuredState(); 
     } 
    } 

    for (View child:qResult){ 
     if (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED) 
      child.measure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST), 
        MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST)); 
    } 

    maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); 
    maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); 

    // Report our final dimensions. 
    setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState), 
      resolveSizeAndState(maxHeight, heightMeasureSpec, 
        childState << MEASURED_HEIGHT_STATE_SHIFT)); 


} 


@Override 
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
    final int count = getChildCount(); 

    int parentLeft = left + getPaddingLeft(); 
    int parentRight = right - getPaddingRight(); 

    final int parentTop = top + getPaddingTop(); 
    final int parentBottom = bottom - getPaddingBottom(); 

    if (qImage == null) return; 

    qImage.layout(parentLeft, parentTop, parentRight, parentBottom); 


    Iterator<Point> loc = qLoc.iterator(); 
    for (View child:qResult) { 
     Point p = loc.next(); 
     if (child.getVisibility() != GONE) { 

      int width = child.getMeasuredWidth(); 
      int height = child.getMeasuredHeight(); 
      Point locOnView = qImage.projectOnView(p); 

      width = (width < (int) Math.max(parentRight - (int) locOnView.x, locOnView.x - parentLeft)) ? 
      width : (parentLeft + parentRight)/2; 
      height = (height < (int) Math.max(parentBottom - (int) locOnView.y, locOnView.y - parentTop)) ? 
      height : (parentBottom + parentTop)/2; 

      int x = (width < (parentRight - (int) locOnView.x)) ? (int) locOnView.x : (parentRight - width); 
      int y = (height < parentBottom - (int) locOnView.y) ? (int) locOnView.y : (parentBottom - height); 


      // Place the child. 
      child.layout(x, y, x + width, y + height); 
     } 
    } 

} 

}

它應該顯示的圖像的頂部有些武斷認爲,考慮到該視圖的位置,當我使用GridView作爲任意視圖,儘管我已經爲GridView定義了一定的寬度,但它被強制爲具有與框架一樣大的寬度。在措施階段,我改變了模式

MeasureSpec.AT_MOST 

的寬度和高度疊加的看法,但這似乎並沒有工作,可有人請幫助。

這裏是XML,我從

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/result_view" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:columnWidth="@dimen/result_view_column_width" 
android:numColumns="2" 
android:verticalSpacing="2dp" 
android:horizontalSpacing="2dp" 
android:stretchMode="none" 
android:gravity="center" 
android:layout_margin = "2dp" 
android:background="@drawable/solid_with_shadow" /> 

回答

0

大量的試驗和錯誤後膨脹的GridView控件,用

measureChild(child, MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST), 
       MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST)); 

工作對我來說更換

child.measure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST), 
       MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST)); 

,我不知道爲什麼,但一個瘋狂的猜測會調用小孩不讀所有的XML道具,但衡量Chi ld(小孩,...)。