Activity#onWindowFocusChangedメソッドは, ビューが見える状態になってから呼ばれているようなので, ここで取得する.
- onWindowFocusChangedメソッドをオーバーライドする.
- findViewByIdメソッドで, サイズを取得したいビューのインスタンスを取得する.
- View#getWidthメソッド, getHeightメソッドでサイズを取得する.
package com.moonlight_aska.android.getviewsize01;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class GetViewSize01 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Viewのサイズを取得 → 値の取得不可
TextView text = (TextView)findViewById(R.id.text_id);
Log.v("onCreate", "TextView width=" + text.getWidth() + ", height=" + text.getHeight());
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
// Viewのサイズを取得
TextView text = (TextView)findViewById(R.id.text_id);
Log.v("onWindowFocusChanged", "TextView width=" + text.getWidth() + ", height=" + text.getHeight());
}
}