當按鈕初始時,Android是獲取不到高度、寬度、坐標值的,需要通過延時的方式獲取,代碼如下:
定義全局按鈕對像
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();