View aboutButton = findViewById(R.id.about_button);
w = aboutButton.getWidth();        
h = aboutButton.getHeight();

解决方案 »

  1.   

    只有在控件显示之后,这样调用才可以的,如果在 onCreate 中调用是不行的
      

  2.   

       在Oncreate 不能调用 ?  是说在setContentView()之后调用吗 ?
      

  3.   


    Oncreate还没结束呢 按钮还没生出来呢 你就要知道宽高 当然是0
      

  4.   

    什么时候需要获取的时候再获取呗,只要别再onCreate里获取就行
      

  5.   


     我现在有个需求是:做一个工具栏,里面有很多控件。要让每个控件之间的Margin 。
      得到屏幕宽度减去控件自身的宽度。 现在控件宽度我就是得不到。
      

  6.   

    在创建之后是能够得到的!就是在oncreate之后
      

  7.   

    onWindowFocusChanged这个时候应该可以了
      

  8.   

    你是通过布局文件创建的吧?
    要不改成动态创建布局吧,就是创建一个linearlayout,自己通过addview的方式将new出来的控件一个一个加到这个layout里,然后LinearLayout.LayoutParams参数你可以指定,也就是说
    public int bottomMargin The bottom margin in pixels of the child.
    public int leftMargin The left margin in pixels of the child.
    public int rightMargin The right margin in pixels of the child.
    public int topMargin The top margin in pixels of the child.
    这四个margin你自己写死了,那也就知道了。
      

  9.   

    还不行就在onResume或者onStart里获取宽高试试
      

  10.   

    onResume和onStart似乎不够晚我记得
      

  11.   

    学习了,查了一下,好像是挺有用的
    http://blog.csdn.net/wanjunfu/archive/2010/11/23/6029297.aspx
      

  12.   

    突然又想到了个方法,比如说有个textview,那可以在onCreate里直接就调用setfocus,然后textview的onfocuschange里来获取宽高....不知道行不行呢
      

  13.   


     activity 加载完毕,还能不能设置各个控件的Margin 呢 ?