この技は本当にいるのかよくわからないです。
本当に自分が忘れないためのメモです、スミマセン。
ViewGroupを継承したクラスである必要があります。
本当に自分が忘れないためのメモです、スミマセン。
ViewGroupを継承したクラスである必要があります。
private int _widthMeasureSpec = 0;
private int _heightMeasureSpec = 0;
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT
, ViewGroup.LayoutParams.FILL_PARENT);
//inflaterを使ってxmlのレイアウトをViewに反映する
LayoutInflater inflater = (LayoutInflater)getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.layout, null);
addViewInLayout(v, -1, lp);
//子要素に必要な大きさを計測させる
int padding = 0;
int childWidthSpec = ViewGroup.getChildMeasureSpec(_widthMeasureSpec, padding * 2, lp.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(_heightMeasureSpec, padding * 2, lp.height);
v.measure(childWidthSpec, childHeightSpec);
//子要素の描画範囲でレイアウトを作成する
v.layout(padding, padding, padding + v.getMeasuredWidth(), padding + v.getMeasuredHeight());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
_widthMeasureSpec = widthMeasureSpec;
_heightMeasureSpec = heightMeasureSpec;
}


コメント (1)
これがやりたくて2日間悩んでました。
この方法でバッチリでした!
ありがとうございます!
投稿者: pop | 2012年01月11日 23:34
日時: 2012年01月11日 23:34