How to retrieve the dimensions of a view?

前端 未结 16 1901
小蘑菇
小蘑菇 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:39

    You can use a broadcast that is called in OnResume ()

    For example:

    int vh = 0;   
    int vw = 0;
    
    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.maindemo);  //<- includes the grid called "board"
    
          registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    TableLayout tl = (TableLayout) findViewById(R.id.board);  
                    tl = (TableLayout) findViewById(R.id.board);
                    vh = tl.getHeight();       
                    vw = tl.getWidth();               
                }
          }, new IntentFilter("Test"));
    }
    
    protected void onResume() {
            super.onResume();
    
            Intent it = new Intent("Test");
            sendBroadcast(it);
    
    }
    

    You can not get the height of a view in OnCreate (), onStart (), or even in onResume () for the reason that kcoppock responded

提交回复
热议问题