0

我有以下類別:重力孩子的TextView不使用自定義的ViewGroup工作

public class MyGridView extends ViewGroup { 
... 
@Override 
public void onSizeChanged(int xNew, int yNew, int xOld, int yOld) { 

super.onSizeChanged(xNew, yNew, xOld, yOld); 
// do calculations for drawing bounds of child views 
} 

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    View child; 
Rect rect; 

for (int i=0; i < getChildCount(); i++) { 
    child = getChildAt(i); 

    rect = getBoundsAtChildIndex(i) 
     child.layout(rect.left, rect.top, rect.right, rect.bottom); 
    } 
} 
... 
} 

public class MyAdapter { 

public void setMyGridView(MyGridView myGridView) { 

// add a single TextView to my grid view 
textView = createTextView(context); 
addTextViewToGrid(textView); 
container.add(textView); 
} 

private TextView createTextView(Context context) { 
    TextView textView = new TextView(context); 

    textView.setText("1"); 
    textView.setTextColor(0xffffffff); 

    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f); 
    textView.setTypeface(typeface); 

    textView.setGravity(Gravity.CENTER | Gravity.FILL); 
// textView.setGravity(Gravity.RIGHT); // other tries to see gravity 
// textView.setGravity(Gravity.FILL); 

    return textView; 
} 

private void addTextViewToGrid(TextView textView) { 
    myGridViewPtr.addView(textView, ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.MATCH_PARENT); 
} 
... 
} 

我創建一個自定義網格視圖和適配器,在其子畫到屏幕到正確的大小和位置。但是,它們在createTextView()中設置的引力未被遵守。

我可以設置一個背景圖像textView,並看到它填補其在網格中的空間。

相比之下,textView中的文本總是繪製在它的左上角,它始終保持在我設置爲12sp的文本大小,而不是縮放以適合使用Gravity.FILL。

優選地,文本將縮放以適合並居中在文本視圖內。

EDIT

我曾經在的ViewGroup加入下列方法onMeasure():

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    int width = getMeasuredWidth(); 
    int height = getMeasuredHeight(); 

    doUpdateOnSizeChanged(width, height); 

    for (int i = 0; i < getChildCount(); i++) 
     getChildAt(i).measure((int) viewDim.x, (int) viewDim.y); 

    setMeasuredDimension(width, height); 
} 

孩子們應該是相同的高度,寬度爲彼此。 ViewGroup的android示例代碼比我需要的更復雜。例如,我沒有得到所有兒童寬度,高度的最大值。

對於我的onMeasure(),ViewDim中的子寬度,高度是寬度,在doUpdateOnSizeChanged中計算的高度整數小於getMeasuredWidth,getMeasuredHeight。

現在結果是文本在TextView的左下角對齊。

+0

瞭解onMeasure – pskink

+0

由於作爲上衡量去,從我研究過的例子,它主要是測量,如只是閱讀,爲了計算視圖組本身的寬度和高度,然後最後調用setMeasured。我不完全清楚哪種方法會對孩子的測量做些什麼。 –

回答

1

onMeasure需要稍作修改,基本上需要依靠MeasureSpec:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

    int width, height; 
    int cWidth1, cHeight1; 

    width = MeasureSpec.getSize(widthMeasureSpec); 
    height = MeasureSpec.getSize(heightMeasureSpec); 

    doUpdateOnSizeChanged(width, height); 


    cWidth1 = (int)viewDim.x; cHeight1 = (int)viewDim.y; 

    measureChildren(MeasureSpec.makeMeasureSpec(cWidth1, MeasureSpec.EXACTLY), 
      MeasureSpec.makeMeasureSpec(cHeight1, MeasureSpec.EXACTLY)); 

    setMeasuredDimension(width, height); 
} 
相關問題