実行時にAndroid を拡張する myのheight
とを次のように変更しています。width
CustomView
View
@Override
public void onGlobalLayout() {
if(!initialized) {
int containerHeight = instance.getHeight();
int containerWidth = instance.getWidth();
myView.getLayoutParams().height = (int) (containerHeight * HEIGHT_RATIO);
myView.getLayoutParams().width = (int) (containerWidth * WIDTH_RATIO);
instance.getViewTreeObserver().removeGlobalOnLayoutListener(this);
initialized = true;
}
}
このコードはcontainer view
コンストラクターにあります。
さらに、私のCustomView
onMeasure()
は次のとおりです。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// maximum width we should use
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
}
結果は次のとおりです。
width
と指定した場所はheight
、緑色の長方形と同じサイズです。
私の質問は:カスタム ビュー (赤い四角形) の実際のサイズが、入力として指定したサイズと同じでないのはLayoutParams
なぜですか?