当按钮初始时,是获取不到高度、宽度、坐标值的,需要通过延时的方式获取,代码如下:
定义全局按钮对像
private Button bt1;
然后在onCreate里面:
setContentView(R.layout.main);
bt1 = (Button) findViewById(R.id.Button01);
new Thread()
{
@Override
public void run()
{
synchronized(this)
{
try
{
wait(1000); //1秒
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Log.i("Test", "高度:" + bt1.getHeight());
Log.i("Test", "宽度:" + bt1.getWidth());
Log.i("Test", "坐标x:" + bt1.getRight());
Log.i("Test", "坐标y:" + bt1.getTop());
}
}.start();