How to retrieve the dimensions of a view?

前端 未结 16 1995
小蘑菇
小蘑菇 2020-11-22 01:09

I have a view made up of TableLayout, TableRow and TextView. I want it to look like a grid. I need to get the height and width of this grid. The methods

16条回答
  •  我在风中等你
    2020-11-22 01:26

    Simple Response: This worked for me with no Problem. It seems the key is to ensure that the View has focus before you getHeight etc. Do this by using the hasFocus() method, then using getHeight() method in that order. Just 3 lines of code required.

    ImageButton myImageButton1 =(ImageButton)findViewById(R.id.imageButton1); myImageButton1.hasFocus();

    int myButtonHeight = myImageButton1.getHeight();

    Log.d("Button Height: ", ""+myButtonHeight );//Not required

    Hope it helps.

提交回复
热议问题